user4803090
user4803090

Reputation: 59

Pull Youtube account info via access token?

I am working with OAuth for the first time and playing around with the Youtube one. I got the following code:

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>';
}

How do I obtain the Youtube channels ID, username, subscribers count and such via the access token, if possible at all?

Upvotes: 2

Views: 724

Answers (1)

Janyk
Janyk

Reputation: 571

You can use this access token to access other data endpoints in the Data API v3 by specifying mine=true as parameter. For more info the channel list endpoint, check out the documentation: https://developers.google.com/youtube/v3/docs/channels/list

Upvotes: 3

Related Questions