2016-02-24 66 views
1

我希望將視頻上傳到youtube中的特定播放列表中。我的頻道中有一個播放列表,但是當我上傳視頻時,它上傳的內容爲channel而不是playlist將視頻上傳到YouTube上的特定播放列表

我想我的播放列表(Title,ID etc)和視頻上傳到我已經創建了怎麼辦that.I該playlist之一已閱讀本關於谷歌,但不能怎麼理解這一點。

這是當前將視頻添加到頻道的腳本。

<?php 

$key = file_get_contents('text.txt'); 

set_include_path(get_include_path() . PATH_SEPARATOR . 'YoutubeAPI'); 
require_once 'Google/Client.php'; 
require_once 'Google/Service/YouTube.php'; 

$application_name = 'My APP NAME'; 
$client_secret = 'SECRET'; 
$client_id = 'CLIENT ID'; 
$scope = array('https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtubepartner'); 

$videoPath = "Video/vdeo.mp4"; 
$videoTitle = "Tutorial"; 
$videoDescription = "A must watch video"; 
$videoCategory = "22"; 
$videoTags = array("youtube", "tutorial"); 


try { 
    // Client init 
    $client = new Google_Client(); 
    //$client->setApplicationName($application_name); 
    $client->setClientId($client_id); 
    $client->setAccessType('offline'); 
    $client->setAccessToken($key); 
    $client->setScopes($scope); 
    $client->setClientSecret($client_secret); 

    if ($client->getAccessToken()) { 

     /** 
     * Check to see if our access token has expired. If so, get a new one and save it to file for future use. 
     */ 
     if ($client->isAccessTokenExpired()) { 
      $newToken = json_decode($client->getAccessToken()); 
      $client->refreshToken($newToken->refresh_token); 
      file_put_contents('text.txt', $client->getAccessToken()); 
     } 

     $youtube = new Google_Service_YouTube($client); 



     // Create a snipet with title, description, tags and category id 
     $snippet = new Google_Service_YouTube_VideoSnippet(); 
     $snippet->setTitle($videoTitle); 
     $snippet->setDescription($videoDescription); 
     $snippet->setCategoryId($videoCategory); 
     $snippet->setTags($videoTags); 

     // Create a video status with privacy status. Options are "public", "private" and "unlisted". 
     $status = new Google_Service_YouTube_VideoStatus(); 
     $status->setPrivacyStatus('public'); 

     // Create a YouTube video with snippet and status 
     $video = new Google_Service_YouTube_Video(); 
     $video->setSnippet($snippet); 
     $video->setStatus($status); 

     // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks, 
     // for reliable connections). Setting it lower leads better recovery (fine-grained chunks) 
     $chunkSizeBytes = 1 * 1024 * 1024; 

     // Setting the defer flag to true tells the client to return a request which can be called 
     // with ->execute(); instead of making the API call immediately. 
     $client->setDefer(true); 

     // Create a request for the API's videos.insert method to create and upload the video. 
     $insertRequest = $youtube->videos->insert("status,snippet", $video); 

     // Create a MediaFileUpload object for resumable uploads. 
     $media = new Google_Http_MediaFileUpload(
       $client, $insertRequest, 'video/*', null, true, $chunkSizeBytes 
     ); 
     $media->setFileSize(filesize($videoPath)); 


     // Read the media file and upload it chunk by chunk. 
     $status = false; 
     $handle = fopen($videoPath, "rb"); 
     while (!$status && !feof($handle)) { 
      $chunk = fread($handle, $chunkSizeBytes); 
      $status = $media->nextChunk($chunk); 
     } 

     fclose($handle); 

     /** 
     * Video has successfully been upload, now lets perform some cleanup functions for this video 
     */ 
     if ($status->status['uploadStatus'] == 'uploaded') { 
      // Actions to perform for a successful upload 
     } 
     print_r($status); 
     // If you want to make other calls after the file upload, set setDefer back to false 
     $client->setDefer(true); 
    } else { 
     // @TODO Log error 
     echo 'Problems creating the client'; 
    } 
} catch (Google_Service_Exception $e) { 
    print "Caught Google service Exception " . $e->getCode() . " message is " . $e->getMessage(); 
    print "Stack trace is " . $e->getTraceAsString(); 
} catch (Exception $e) { 
    print "Caught Google service Exception " . $e->getCode() . " message is " . $e->getMessage(); 
    print "Stack trace is " . $e->getTraceAsString(); 
} 

在此先感謝。

回答

1

是的,它是正確的。上傳視頻時,它會重定向到您的頻道。您無法直接將視頻上傳到播放列表。從頻道,你可以把視頻放在eplaylist。要做到這一點,你必須執行下列2(兩)個步驟:

第1步:獲取相應的播放列表ID

調用playlist.list方法來檢索當前已驗證用戶的播放列表渠道。以上用於檢索當前用戶播放列表的示例請求演示了此請求。調用API的應用程序可以處理API響應以顯示播放列表列表,並將每個播放列表的ID用作關鍵字。

第2步:將視頻添加到播放列表

調用playlistItems.insert方法將視頻添加到播放列表。此請求必須使用OAuth 2.0進行授權。請求正文是指定至少以下值的播放列表項資源:

snippet.playlistId標識要向其添加最喜愛視頻的播放列表。這是您在步驟1中獲得的播放列表ID。 snippet.resourceId.kind包含值youtube#video。 snippet.resourceId.videoId標識您添加爲收藏夾的視頻。該屬性值是唯一的YouTube視頻ID。

要完成在API瀏覽器的要求,你需要爲snippet.playlistIdsnippet.resourceId.videoId屬性中設置的值。 https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.insert?part=snippet

+0

工作成功,但有單獨的2個身份驗證請求。 因此,我可以在相同的請求中使用它嗎? 我的意思是我想上傳,然後插入播放列表中的同一個請求,而不需要重新認證或刷新頁面。 – semsem

相關問題