当前位置: 动力学知识库 > 问答 > 编程问答 >

How to get YouTube channel information from Google+ ID

问题描述:

The site is doing Google OAuth 2 authorization.

scope: "profile https://www.googleapis.com/auth/youtube "

After login, Google gave me this user data:

{

id: 107055280208390515,

name: Name,

link: https://plus.google.com/107055208390515542,

picture: https://lh3.googleusercontent.com/-q1Smh9d8d0g/AAAAAAAAAAM/AAAAAAAAAAA/3YaTIPc/photo.jpg,

locale: en

}

But no YouTube channel information is included in the results. Is there a different scope or API? Searched Google documentation & found nothing.

网友答案:

Well, authenticating is just the first step (Google+ oAuth doesn't return any Youtube channel information) I believe to access authenticated user's channel information you need to use the Youtube APIs ( https://developers.google.com/youtube/v3/docs/channels).

You didn't specify what platform you are going to use, but here is a modified Google's sample code to access Youtube channel information using PHP:

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

I haven't tested this code yet, but I hope you get the general idea.

More channel attributes can be found on the link I gave you above.

分享给朋友:
您可能感兴趣的文章:
随机阅读: