添加文件名到YouTube API上传

时间:2017-12-19 16:01:37

标签: php google-api youtube-api

我一直在使用API​​上传到YouTube的视频的“Raw:Unknown”文件名。看看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));

1 个答案:

答案 0 :(得分:0)

我知道这个话题很老,但是如果别人碰巧碰到同一件事,我想节省他们的时间。

首先,您不能在此请求中提交“ 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()返回请求的新实例。