【问题标题】:Upload video to specific playlist将视频上传到特定播放列表
【发布时间】:2017-03-23 16:34:49
【问题描述】:

我正在使用 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();

 }

【问题讨论】:

    标签: php youtube google-api youtube-api youtube-data-api


    【解决方案1】:

    根据这个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 帖子,可能会有所帮助:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 2011-02-27
      • 2015-01-10
      • 2018-07-29
      • 1970-01-01
      相关资源
      最近更新 更多