【问题标题】:Add filename to YouTube API upload将文件名添加到 YouTube API 上传
【发布时间】:2017-12-19 16:01:37
【问题描述】:

我使用 API 上传到 YouTube 的视频的文件名是“原始:未知”。查看 YouTube API 库,我看到有一个

Google_Service_YouTube_VideoFileDetails()

你可以设置文件名的方法,但是当我尝试它时,我收到以下错误:

Caught Google service Exception 400 message is {
error": {
"errors": [
{
"domain": "youtube.part",
"reason": "unexpectedPart",
"message": "{0}",
"locationType": "parameter",
"location": "part"
}
],
"code": 400,
"message": "{0}"
}
} 

这是我的代码:

$snippet = new Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle("Test title");
        $snippet->setDescription("Test description");
        $snippet->setTags(array("tag1","tag2"));
        $snippet->setCategoryId("27");
        $status = new Google_Service_YouTube_VideoStatus();
        $status->setPrivacyStatus('private');
        $status->setPublishAt("2019-08-13T13:13:13.1Z");
        $status->setEmbeddable(false);
        $fileDetails = new Google_Service_YouTube_VideoFileDetails();
        $fileDetails->setFileName("WI_YOUTUBE_COMMA_DESCRIPTION.mp4");
        $video = new Google_Service_YouTube_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);
        $video->setFileDetails($fileDetails);     
        $chunkSizeBytes = 1 * 1024 * 1024; 
        $client->setDefer(true);
        $insertRequest = $youtube->videos->insert("status,snippet,fileDetails", $video,
            array('onBehalfOfContentOwner' => $contentOwnerId,
                'onBehalfOfContentOwnerChannel' => $channelId));

【问题讨论】:

  • 您是否尝试过将“Slug:”+ your_filename 添加到您的 HTTP 标头中,如 SO post 中所示
  • 如果您使用的是 google api 客户端库,您将如何以及将其放在哪里?
  • 你找到答案了吗?我也有同样的问题。

标签: php google-api youtube-api


【解决方案1】:

我知道这个话题很老了,但如果他们遇到同样的事情,我想为其他人节省时间。

首先,您无法在此请求中提交“fileDetails”,这就是您收到此错误的原因。 noogui 是正确的,'Slug' 是解决方案,这就是你修改代码的方式:

        $snippet = new Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle("Test title");
        $snippet->setDescription("Test description");
        $snippet->setTags(array("tag1","tag2"));
        $snippet->setCategoryId("27");

        $status = new Google_Service_YouTube_VideoStatus();
        $status->setPrivacyStatus('private');
        $status->setPublishAt("2019-08-13T13:13:13.1Z");
        $status->setEmbeddable(false);

        $video = new Google_Service_YouTube_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);

        $chunkSizeBytes = 1 * 1024 * 1024; 
        $client->setDefer(true);
        $insertRequest = $youtube->videos->insert("status,snippet", $video,
            array('onBehalfOfContentOwner' => $contentOwnerId,
                'onBehalfOfContentOwnerChannel' => $channelId));
        $insertRequest = $insertRequest->withHeader("Slug", "WI_YOUTUBE_COMMA_DESCRIPTION.mp4");

这仅在 setDefer 设置为 true 时有效,否则请求会在您调用 insert() 的瞬间发送。

另外,withHeader() 返回请求的一个新实例。

【讨论】:

  • 对于 youtube python API,要设置文件名,请将“Slug”放在请求标头中,例如:insert_request.headers["Slug"] = fileName
【解决方案2】:

对于 youtube python API,要设置文件名,请将“Slug”放在请求标头中,例如:

insert_request.headers["Slug"] = fileName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 2020-04-26
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多