dongmou3615 2014-03-23 00:20
浏览 39

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

I have a simple user system with basic information: email and password. Instead of using the Google api as a "Sign in" feature:

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?

I have written a few PHP Classes that take care of Google's OAuth Flow, as well as storing the access token, expiration time, and the refresh token in a database (You may think "Don't reinvent the wheel", but i prefer to learn as well by writing these classes).

As i kept going through my logic and how i would lay everything out, i just did not understand how i Link the user's account to my previous account. EG: When they login, how do i get the user's Linked channels?

Here is some of my code from my 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'];
}

And some from my TokenStorage Class:

// 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;
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dpi10335 2014-04-02 03:34
    关注

    Problem

    You want to integrate your existing login solution and link users Youtube accounts.

    Recommendation

    Why not use the PHP API over at Youtube? (API) Collect the login and Youtube account links into a new table and ask the user to link their accounts (when they sign in using the Youtube account) by logging into both your account and the new login? After which, the accounts can be maintained in sync because you will extend your login table with a new column of information or linked to a related table (of encrypted Youtube links). At least this is the big idea. I am sure there will be several challenges to make this work but I have seen it on other websites.

    In essence build a second login flow for the Youtubers, connect via the API. Then ask them to login to their normal account and link the logins using a column in the existing table or using a new linked table with a foreign key to allow multiple Youtube pages/accounts. That would be my initial approach. The link provided above is the connection to the Youtube PHP API. I haven't needed to do this myself but if you need more precise help please let me know.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错