如何在Google Oauth PHP之后获取频道ID或URL [英] How to Get Channel id or url after Google Oauth PHP

查看:85
本文介绍了如何在Google Oauth PHP之后获取频道ID或URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用youtube登录代码编写应用程序系统.但是我有一个问题,当我获得oauth 2.0的必要授权后,我想以字符串形式获取所选的YouTube频道ID,但是我做不到,请有人帮帮我.

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";在下面的代码中使用它

$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>";

推荐答案

使用OAUTH 2,您可以获取所请求范围的refresh_token和access_token. 如果您想使用授权用户使用其access_token获取YouTube频道ID,则可以发送请求:

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

在上面的请求中,您需要替换字符串"HereYourAccessToken". 响应是一个JSON字符串.频道ID位于名为"items ['id']"(或items.id)的字段中.

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).

请注意,access_token将在1小时后过期.如果是这样,请使用refresh_token获取一个新的, 在所有者撤消之前,refresh_token有效.

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.

这篇关于如何在Google Oauth PHP之后获取频道ID或URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆