2014-07-20 3 views
3

사이트에서 Google OAuth 2 인증을하고 있습니다.Google+ ID에서 YouTube 채널 정보를 얻는 방법

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

로그인 후, 구글은 저에게이 사용자 데이터 주었다

{ 
    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 설명서 &은 아무 것도 발견되지 않았습니다.

답변

3

음, 인증은 단지 첫 번째 단계입니다. Google+ oAuth는 Youtube 채널 정보를 반환하지 않습니다. Youtube API (https://developers.google.com/youtube/v3/docs/channels)를 사용해야하는 인증 된 사용자의 채널 정보에 액세스 할 수 있다고 생각합니다.

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

I의 천국 :

현재 당신이 사용하고자하는 어떤 플랫폼 지정하지 않았지만이 PHP를 사용하여 유튜브 채널 정보에 액세스 할 수있는 수정 된 구글의 샘플 코드 아직이 코드를 테스트하지는 않았지만 일반적인 생각을 갖기를 바랍니다.

위의 링크에서 더 많은 채널 속성을 찾을 수 있습니다.

관련 문제