Harun Sivaslı
Harun Sivaslı

Reputation: 31

How to Get Channel id or url after Google Oauth PHP

i am trying to code an application system with youtube login. but i have an issue After i get required authorisation with oauth 2.0 i want to get choosen youtube channel id in a string but i could not do it can somebody please help me.

    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->setApplicationName("BYTNETWORK");
    $client->setScopes(array('https://www.googleapis.com/auth/userinfo.profile',
     'https://www.googleapis.com/auth/yt-analytics.readonly' 

      ));

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

so after this i want a string like

$channelid = "xxxxx"; to use it in code below

$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/$channelid?alt=json');
$data = json_decode($data, true);
$stats_data = $data['entry']['yt$statistics'];
$medya = $data['entry']['media$thumbnail'];
//yt$username kısmından kanal adı parse edilir

/**********************************************************/
echo "<img src='$medya[url]'></img><br>";
echo "Subscribers: $stats_data[subscriberCount]<br>";
echo "Views: $stats_data[totalUploadViews]<br>";

Upvotes: 1

Views: 2488

Answers (1)

Als
Als

Reputation: 1415

Using OAUTH 2, you can get a refresh_token and an access_token for the requested scope(s). If you want to get the YouTube channel-id, for an authorized user, using his access_token, then you send the request:

https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&access_token=HereYourAccessToken

In the request above you need to replace the string "HereYourAccessToken". The respone is a JSON string. And the channel-id is in the field named: items['id'] (or items.id).

Note, that an access_token expires after 1 hour. If so, then use the refresh_token to obtain a new one, The refresh_token is valid until revoked by the owner.

Upvotes: 5

Related Questions