该网站正在执行Google OAuth 2授权。
scope: "profile https://www.googleapis.com/auth/youtube "
登录后,Google向我提供了该用户数据:
{
id: 107055280208390515,
name: Name,
link: https://plus.google.com/107055208390515542,
picture: https://lh3.googleusercontent.com/-q1Smh9d8d0g/AAAAAAAAAAM/AAAAAAAAAAA/3YaTIPc/photo.jpg,
locale: en
}
但是结果中没有YouTube频道信息。是否有其他范围或API?搜索了Google文档,却一无所获。
好吧,身份验证只是第一步(Google+ oAuth不会返回任何Youtube频道信息),我相信要访问经过身份验证的用户的频道信息,您需要使用Youtube API(https://developers.google.com/youtube/v3/ docs / channels)。
您没有指定要使用的平台,但是这是经过修改的Google的示例代码,可使用PHP访问Youtube频道信息:
<?php
// Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* Google Developers Console <https://console.developers.google.com/>
* For more information about using OAuth 2.0 to access Google APIs, please see:
* <https://developers.google.com/youtube/v3/guides/authentication>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
try {
// Call the channels.list method to retrieve information about the
// currently authenticated user's channel.
$channelsResponse = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
//example: print title and descriptions for the user default (first)
渠道
$defChannel = $channelsResponse->getItems()[0]->getBrandingSettings()->getChannel();;
$channelTitle= $defChannel->title;
$channelDesc = $defChannel->description;
$htmlBody .= "<h3>Your Channel</h3>";
$htmlBody .= sprintf('Title: %s <br />',$channelTitle);
$htmlBody .= sprintf('Descriptions: %s <br />',$channelDesc);
END;
?>
<!doctype html>
<html>
<head>
<title>Channel Info</title>
</head>
<body>
<?=$htmlBody?>
</body>
</html>
我尚未测试此代码,但希望您能大致了解。
在上面我给你的链接中可以找到更多的频道属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句