youtube - 如何从 Google+ ID 获取 YouTube channel 信息

标签 youtube youtube-api

该网站正在进行 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 channel 信息。是否有不同的范围或 API?搜索了 Google 文档,但一无所获。

最佳答案

嗯,身份验证只是第一步(Google+ oAuth 不会返回任何 Youtube channel 信息),我相信要访问经过身份验证的用户的 channel 信息,您需要使用 Youtube API ( https://developers.google.com/youtube/v3/docs/channels )。

您没有指定要使用什么平台,但这里有一个修改后的 Google 示例代码,用于使用 PHP 访问 Youtube channel 信息:

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

channel

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

我还没有测试过这段代码,但我希望你能了解总体思路。

更多 channel 属性可以在我上面给您的链接中找到。

关于youtube - 如何从 Google+ ID 获取 YouTube channel 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24847907/

相关文章:

html - 如何从youtube html页面提取视频时长?

youtube - 首次使用youtube-dl下载后,我对youtube的访问被阻止

javascript - YouTube API上传视频并设置付费产品展示位置字段

youtube-api - 在没有视频 ID 的情况下初始化 YouTube iframe 播放器

youtube-api - 信息API YouTube

json - YouTube API |没有得到完整的JSON响应

Android Webview 视频问题。视频正在 youtube iframe 中被裁剪。

javascript - 自动播放Google云端硬盘中的视频并在完成JavaScript时发出警报

android - 如何在 ListView 中刷新 Youtube 播放器 View onItemClickListener()

xml - 从 YouTube 评论提要中获取用户拇指?