通过访问令牌提取Youtube帐户信息?

时间:2015-05-02 19:50:41

标签: php oauth-2.0 youtube-api

我第一次与OAuth合作,并与Youtube一起玩。我得到了以下代码:

if(isset($_GET['code'])) {

    $code = $_GET['code'];

    $url = 'https://accounts.google.com/o/oauth2/token';

    $params = array(
        "code" => $code,
        "client_id" => "XXX",
        "client_secret" => "YYY",
        "redirect_uri" => "URL",
        "grant_type" => "authorization_code"
    );

    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

    $json_response = curl_exec($curl);
    curl_close($curl);
    $authObj = json_decode($json_response);

    echo "access_token: " . $authObj->access_token;
    echo "token_type: " . $authObj->token_type;
    echo "expires_in: " . $authObj->expires_in;
    echo "refresh_token: " . $authObj->refresh_token;

    $msg = '<p class="bg-success msg-padding"><b>Success:</b> You have successfully linked your Youtube account.</p>';

}

if(isset($_GET['error'])) {
    $msg = '<p class="bg-danger msg-padding"><b>Error:</b> You have canceled the Youtube account linking process.</p>';
}

如果可能的话,如何通过访问令牌获取Youtube频道ID,用户名,订阅者数等?

1 个答案:

答案 0 :(得分:3)

通过将mine = true指定为参数,您可以使用此访问令牌访问Data API v3中的其他数据端点。 有关频道列表端点的更多信息,请查看文档:{​​{3}}