将Google OAuth API与现有用户系统集成


Integrating Google OAuth API with existing user system?

我有一个简单的用户系统的基本信息:电子邮件和密码。而不是使用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类,照顾谷歌的OAuth流,以及存储访问令牌,过期时间和刷新令牌在数据库中(你可能会认为"不要重新发明轮子",但我更喜欢通过编写这些类来学习)。

当我不断通过我的逻辑和我将如何布局一切,我只是不明白我如何链接用户的帐户到我以前的帐户。例如:当他们登录时,我如何获得用户的链接频道?

下面是我的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;
            }
        }
    }
}

问题

你想整合你现有的登录解决方案和链接用户的Youtube帐户。

推荐

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

本质上是为youtuber构建第二个登录流,通过API连接。然后要求他们登录到他们的正常帐户,并使用现有表中的列链接登录,或使用带有外键的新链接表,以允许多个Youtube页面/帐户。这将是我最初的方法。上面提供的链接是到Youtube PHP API的连接。我还没有需要这样做自己,但如果你需要更精确的帮助,请告诉我。