【问题标题】:How can I update a YouTube Video? | PHP如何更新 YouTube 视频? | PHP
【发布时间】:2018-10-10 00:15:12
【问题描述】:

我尝试更新 YouTube 视频。我在站点https://developers.google.com/youtube/v3/code_samples/php#update_a_video 上使用该函数,并尝试将其复制到我的代码中,但如果我调用listVideos(),我会得到Google_Service_YouTube_VideoListResponse,但我需要一个Google_Service_YouTube_Video。我怎样才能得到它?

我的 PHP 代码:

$youtube = new Google_Service_YouTube($client);
$videoid = "******Video ID********";
$new = "******* TEXT *********";
$videoinfo = $youtube->videos->listVideos('snippet', array('id' => $videoid));
$videoSnippet = $videoinfo[0]['snippet'];
$description = $videoSnippet['description'] . $new;
$videoSnippet['description'] = $description;
$youtube->videos->update("snippet", $videoSnippet);

错误:

: Uncaught TypeError: Argument 2 passed to 
Google_Service_YouTube_Resource_Videos::update() must be an instance of 
Google_Service_YouTube_Video, instance of 
Google_Service_YouTube_VideoSnippet given, called in 
P:\Apache24\htdocs\*********** on line 232 and defined in 
P:\Apache24\htdocs\*******\Google Api System\vendor\google\apiclient- 
services\src\Google\Service\YouTube\Resource\Videos.php:309 
Stack trace:
0 P:\Apache24\htdocs\*********: Google_Service_YouTube_Resource_Videos- 
>update('snippet', Object(Google_Service_YouTube_VideoSnippet ))
1 P:\Apache24\htdocs\********\*********\index.php(438): 
require('P:\\Apache24\\htd...')

【问题讨论】:

  • $videoid = "******视频 ID********;您忘记用 " $videoid = "******视频 ID** 结束******";
  • @g33k 我只在stackoverflow中忘记了它 :) 在我的代码中我写了 $videoid = "******Video ID********";
  • $videoid 的字面意思是******Video ID******** 还是就在这里?!

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


【解决方案1】:

你必须使用

  $video = $listResponse[0];
  $updateResponse = $youtube->videos->update("snippet", $video);

因为在documentation

// Since the request specified a video ID, the response only
      // contains one video resource.
      $video = $listResponse[0];
      $videoSnippet = $video['snippet'];
      $tags = $videoSnippet['tags'];

      // Preserve any tags already associated with the video. If the video does
      // not have any tags, create a new list. Replace the values "tag1" and
      // "tag2" with the new tags you want to associate with the video.
      if (is_null($tags)) {
        $tags = array("tag1", "tag2");
      } else {
        array_push($tags, "tag1", "tag2");
      }

      // Set the tags array for the video snippet
      $videoSnippet['tags'] = $tags;

      // Update the video resource by calling the videos.update() method.
      $updateResponse = $youtube->videos->update("snippet", $video);

【讨论】:

  • 然后我得到错误“代码”:400,“消息”:“请求元数据无效。”但我认为这是正确的方式:)
  • 错误:{ "error": { "errors": [ { "domain": "youtube.video", "reason": "invalidVideoMetadata", "message": "请求元数据是无效。”,“locationType”:“其他”,“location”:“body”}],“code”:400,“message”:“请求元数据无效。” } }
  • 你在这样做吗 // 检查是否存在所需范围的身份验证令牌 $tokenSessionKey = 'token-' 。 $client->prepareScopes();
  • // 创建一个带有标题、描述、标签和类别 ID 的 sn-p // 创建一个资产资源并设置它的 sn-p 元数据和类型。 // 此示例设置视频的标题、描述、关键字标签和 // 视频类别。 $sn-p = new Google_Service_YouTube_VideoSnippet(); $sn-p->setTitle("测试标题"); $sn-p->setDescription("测试描述"); $sn-p->setTags(array("tag1", "tag2"));
猜你喜欢
  • 2013-08-03
  • 2013-10-23
  • 1970-01-01
  • 2011-07-18
  • 2011-07-10
  • 1970-01-01
  • 2013-05-17
  • 2012-11-08
  • 1970-01-01
相关资源
最近更新 更多