2016-11-09 87 views
0

我使用YouTube數據API將視頻上傳到我的頻道。這工作完美,但當我上傳視頻時,它會上傳到我的頻道。我希望它在給定的播放列表中上傳(我有一些已經創建的播放列表)。有沒有什麼辦法可以讓playlistId在下面的代碼中。我不想使用playlistitem.insert。
由於事先將視頻上傳到特定播放列表

$OAUTH2_CLIENT_ID = 'ds'; 
    $OAUTH2_CLIENT_SECRET = 'sd'; 

$client = new Google_Client(); 
$client->setClientId($OAUTH2_CLIENT_ID); 
$client->setClientSecret($OAUTH2_CLIENT_SECRET); 
$client->setScopes('https://www.googleapis.com/auth/youtube'); 
// Define an object that will be used to make all API requests. 
$youtube = new Google_Service_YouTube($client); 

$tokenExisted = $this->Token->find('first'); 

if(!empty($tokenExisted)){ 

$token = $tokenExisted['Token']['token']; 
$refreshToken = $tokenExisted['Token']['ref_token']; 
$client->refreshToken($refreshToken); 
$token = $client->getAccessToken(); 

} 





if (isset($token)) { 

    $client->setAccessToken($token); 

} 

if ($client->isAccessTokenExpired()){  

$refreshToken = $tokenExisted['Token']['ref_token']; 
$client->refreshToken($refreshToken); 
$token = $client->getAccessToken(); 

}else{ 

$token = $client->getAccessToken(); 
} 
$token=json_encode($token); 

// Check to ensure that the access token was successfully acquired. 
if ($token) { 
try{ 
// REPLACE this value with the path to the file you are uploading. 
$videoPath = $dest . '/' . $img_name . '.' . $image['1']; 


// Create a snippet with title, description, tags and category ID 
// Create an asset resource and set its snippet metadata and type. 
// This example sets the video's title, description, keyword tags, and 
// video category. 
$snippet = new Google_Service_YouTube_VideoSnippet(); 
$snippet->setTitle($input['title']); 
$snippet->setDescription($input['description']); 
//$snippet->playlistId($playlistId); 

// Numeric video category. See 
// https://developers.google.com/youtube/v3/docs/videoCategories/list 
$snippet->setCategoryId($input['category']); 

// Set the video's status to "public". Valid statuses are "public", 
// "private" and "unlisted". 
$status = new Google_Service_YouTube_VideoStatus(); 
$status->privacyStatus = "public"; 

// Associate the snippet and status objects with a new video resource. 
$video = new Google_Service_YouTube_Video(); 
$video->setSnippet($snippet); 
$video->setStatus($status); 

// Specify the size of each chunk of data, in bytes. Set a higher value for 
// reliable connection as fewer chunks lead to faster uploads. Set a lower 
// value for better recovery on less reliable connections. 
$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(false); 

// 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); 

// If you want to make other calls after the file upload, set setDefer back to false 
//$client->setDefer(false); 



} catch (Google_Service_Exception $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())); 
} 


    } else { 
// If the user hasn't authorized the app, initiate the OAuth flow 

$state = mt_rand(); 
$client->setState($state); 
$_SESSION['state'] = $state; 

$authUrl = $client->createAuthUrl(); 

} 

回答

0

根據這一SO answer,你不能直接上傳視頻到播放列表。上傳視頻時,它會重定向到您的頻道,這很正常。一旦它在頻道中上傳,您現在可以將其放入播放列表中。

如果視頻已經上傳,您可以按照此Adding a video to a playlist文檔。您可以使用VideoEntry對象將視頻添加到播放列表。

下面是一個示例代碼,它檢索一個帶有已知條目ID的對象VideoEntry,然後將其添加到對應於PlaylistListEntry對象的播放列表中。由於該請求沒有指定視頻在播放列表中出現的位置,因此新視頻將添加到播放列表的末尾。

$postUrl = $playlistToAddTo->getPlaylistVideoFeedUrl(); 
// video entry to be added 
$videoEntryToAdd = $yt->getVideoEntry('4XpnKHJAok8'); 

// create a new Zend_Gdata_PlaylistListEntry, passing in the underling DOMElement of the VideoEntry 
$newPlaylistListEntry = $yt->newPlaylistListEntry($videoEntryToAdd->getDOM()); 

// post 
try { 
    $yt->insertEntry($newPlaylistListEntry, $postUrl); 
} catch (Zend_App_Exception $e) { 
    echo $e->getMessage(); 
} 

這裏有一個相關的SO職位可能幫助: