【问题标题】:Integrating Google OAuth API with existing user system?将 Google OAuth API 与现有用户系统集成?
【发布时间】: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


    【解决方案1】:

    问题

    您想要集成现有的登录解决方案并链接用户的 Youtube 帐户。

    推荐

    为什么不在 Youtube 上使用 PHP API? (API) 将登录名和 Youtube 帐户链接收集到一个新表中,并要求用户通过登录您的帐户和新登录名来链接他们的帐户(当他们使用 Youtube 帐户登录时)?之后,帐户可以保持同步,因为您将使用新的信息列扩展登录表或链接到相关表(加密的 Youtube 链接)。至少这是一个大想法。我确信要完成这项工作会有一些挑战,但我在其他网站上看到过。

    本质上为 Youtuber 构建第二个登录流程,通过 API 连接。然后要求他们登录到他们的普通帐户并使用现有表中的列或使用带有外键的新链接表链接登录以允许多个 Youtube 页面/帐户。那将是我最初的方法。上面提供的链接是与 Youtube PHP API 的连接。我不需要自己执行此操作,但如果您需要更准确的帮助,请告诉我。

    【讨论】:

    • 谢谢,但是我如何将多个帐户链接到一个用户帐户?我决定不使用他们提供的 API 的原因是它的文档没有我想要的那么好,而且我想要创建自己的 PHP 类的经验。
    猜你喜欢
    • 2015-01-20
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多