【发布时间】:2014-04-30 09:30:28
【问题描述】:
我有一个包含基本信息的简单用户系统:电子邮件和密码。而不是使用 Google api 作为“登录”功能:
Is there any way i can "Link" the user's Youtube Channel to their account?
Is there a way i can Link multiple channels to a user's account? If so, how would i keep track of which user to get data from?
我编写了一些 PHP 类来处理 Google 的 OAuth 流程,以及将访问令牌、过期时间和刷新令牌存储在数据库中(您可能会认为“不要重新发明轮子”,但是我更喜欢通过编写这些课程来学习)。
当我不断了解我的逻辑以及我将如何布置所有内容时,我只是不明白我如何将用户的帐户链接到我以前的帐户。 EG:当他们登录时,我如何获得用户的链接频道?
这是我的 Client.php 中的一些代码
public function authinticate($authorizationCode)
{
// Now we need to do a POST request to get an Access token and a refresh token
$post = array("grant_type" => "authorization_code", "code" => $authorizationCode, "client_id" => $this->clientID, "client_secret" => $this->clientSecret, "redirect_uri" => $this->redirectUri);
$postText = http_build_query($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::Google_OAuth_Token_Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postText);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
//Decode the returned values
$responseArray = json_decode($result, true);
// Set out values
$this->accessToken = $responseArray['access_token'];
$this->refreshToken = $responseArray['refresh_token'];
$this->expiresIn = $responseArray['expires_in'];
}
还有一些来自我的 TokenStorage 类:
// Store the token Data response from google, (IF reresh token is passed with a value, this means that the user JUST authorized our application, so we need to store that value)
public function storeTokenData($channelID, $accessToken, $refreshToken, $expirationTime)
{
$firstRequest = "INSERT INTO tokens (channelID, accessToken, refreshToken, expirationTime) VALUES (?, ?, ?, DATE_ADD(NOW(), INTERVAL $expirationTime SECOND)";
$linkedRequest = "INSERT INTO tokens (channelID, accessToken, expirationTime) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL $expirationTime SECOND)";
if($refreshToken == null)
{
if ($storeTokenData = global $mysqli->prepare($firstRequest))
{
$storeTokenData->bind_param('sss', $channelID, $accessToken, $refreshToken);
if($storeTokenData->execute()) {
// Data stored success...
return true;
} else {
// Something happened...
return false;
}
}
}
else
{
if ($storeTokenData = global $mysqli->prepare($linkedRequest))
{
$storeTokenData->bind_param('ss', $channelID, $accessToken);
if($storeTokenData->execute()) {
// Data stored success...
return true;
} else {
// Something happened...
return false;
}
}
}
}
【问题讨论】:
标签: php oauth youtube youtube-api google-api