2014-05-01 41 views
1

嗨,大家好,我想從我的帳戶,播放列表,類別中獲取視頻,並希望在我的網站上顯示它們。如果我上傳任何視頻管。這應該顯示在我的網站上。請幫我解決一下這個。我是使用yii 1.1如何在yii中使用Oauth 2.0獲取YouTube視頻?

回答

0

1)Create a channel in youtube

2)在您的網頁代碼中嵌入以下代碼。

<script src="http://www.gmodules.com/ig/ifr?url=http://www.google.com/ig/modules/youtube.xml&up_channel=MyChannelName&synd=open&w=320&h=390&title=&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js"></script> 

3)與原來的頻道名稱替換MyChannelName

參考:

YouTube on your site

Embed YouTube Channel

編輯

如果你必須通過OAUTH2做到這一點,嘗試Google APIs Client Library for Youtube

<?php 
// Call set_include_path() as needed to point to your client library. 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php'; 
session_start(); 

/* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console 
    <http://code.google.com/apis/console#access> 
For more information about using OAuth2 to access Google APIs, please visit: 
    <https://developers.google.com/accounts/docs/OAuth2> 
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); 
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 
    FILTER_SANITIZE_URL); 
$client->setRedirectUri($redirect); 

$youtube = new Google_YoutubeService($client); 

if (isset($_GET['code'])) { 
    if (strval($_SESSION['state']) !== strval($_GET['state'])) { 
    die('The session state did not match.'); 
    } 

    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: ' . $redirect); 
} 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

if ($client->getAccessToken()) { 
    try { 
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
     'mine' => 'true', 
    )); 

    $htmlBody = ''; 
    foreach ($channelsResponse['items'] as $channel) { 
     $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads']; 

     $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
     'playlistId' => $uploadsListId, 
     'maxResults' => 50 
    )); 

     $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>"; 
     foreach ($playlistItemsResponse['items'] as $playlistItem) { 
     $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'], 
      $playlistItem['snippet']['resourceId']['videoId']); 
     } 
     $htmlBody .= '</ul>'; 
    } 
    } catch (Google_ServiceException $e) { 
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', 
     htmlspecialchars($e->getMessage())); 
    } catch (Google_Exception $e) { 
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', 
     htmlspecialchars($e->getMessage())); 
    } 

    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $state = mt_rand(); 
    $client->setState($state); 
    $_SESSION['state'] = $state; 

    $authUrl = $client->createAuthUrl(); 
    $htmlBody = <<<END 
    <h3>Authorization Required</h3> 
    <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> 
END; 
} 
?> 

<!doctype html> 
<html> 
    <head> 
    <title>My Uploads</title> 
    </head> 
    <body> 
    <?=$htmlBody?> 
    </body> 
</html> 
+0

嗨harikrishnan我想要使用google api和oauth執行此操作 – Vikky

+0

@Vikky您可以在沒有這個的情況下執行此操作...這很簡單..然後,您爲什麼要這麼做? – Harikrishnan