gpt4 book ai didi

php - 使用 Youtube API V3 和 PHP 将视频上传到 Youtube

转载 作者:IT王子 更新时间:2023-10-28 23:53:50 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 将视频上传到 Youtube。我正在使用 Youtube API v3,并且正在使用最新 checkout 的 Google API PHP 客户端库源代码。
我正在使用
上给出的示例代码 https://code.google.com/p/google-api-php-client/执行身份验证。身份验证顺利进行,但当我尝试上传视频时,我收到 Google_ServiceException,错误代码为 500,消息为 null。

我查看了之前提出的以下问题: Upload video to youtube using php client library v3但是接受的答案没有描述如何指定要上传的文件数据。
我发现了另一个类似的问题Uploading file with Youtube API v3 and PHP ,在评论中提到 categoryId 是强制性的,因此我尝试在代码段中设置 categoryId 但它仍然给出相同的异常。

我还引用了文档站点 (https://developers.google.com/youtube/v3/docs/videos/insert) 上的 Python 代码,但我在客户端库中找不到函数 next_chunk。但是我尝试放置一个循环(在代码片段中提到)以重试获取错误代码 500,但是在所有 10 次迭代中我都得到了相同的错误。

以下是我正在尝试的代码片段:

$youTubeService = new Google_YoutubeService($client);
if ($client->getAccessToken()) {
print "Successfully authenticated";
$snippet = new Google_VideoSnippet();
$snippet->setTitle = "My Demo title";
$snippet->setDescription = "My Demo descrition";
$snippet->setTags = array("tag1","tag2");
$snippet->setCategoryId(23); // this was added later after refering to another question on stackoverflow

$status = new Google_VideoStatus();
$status->privacyStatus = "private";

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

$data = file_get_contents("video.mp4"); // This file is present in the same directory as the code
$mediaUpload = new Google_MediaFileUpload("video/mp4",$data);
$error = true;
$i = 0;

// I added this loop because on the sample python code on the documentation page
// mentions we should retry if we get error codes 500,502,503,504
$retryErrorCodes = array(500, 502, 503, 504);
while($i < 10 && $error) {
try{
$ret = $youTubeService->videos->insert("status,snippet",
$video,
array("data" => $data));

// tried the following as well, but even this returns error code 500,
// $ret = $youTubeService->videos->insert("status,snippet",
// $video,
// array("mediaUpload" => $mediaUpload);
$error = false;
} catch(Google_ServiceException $e) {
print "Caught Google service Exception ".$e->getCode()
. " message is ".$e->getMessage();
if(!in_array($e->getCode(), $retryErrorCodes)){
break;
}
$i++;
}
}
print "Return value is ".print_r($ret,true);

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}

是我做错了什么吗?

最佳答案

我能够使用以下代码进行上传:

if($client->getAccessToken()) {
$snippet = new Google_VideoSnippet();
$snippet->setTitle("Test title");
$snippet->setDescription("Test descrition");
$snippet->setTags(array("tag1","tag2"));
$snippet->setCategoryId("22");

$status = new Google_VideoStatus();
$status->privacyStatus = "private";

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

$error = true;
$i = 0;

try {
$obj = $youTubeService->videos->insert("status,snippet", $video,
array("data"=>file_get_contents("video.mp4"),
"mimeType" => "video/mp4"));
} catch(Google_ServiceException $e) {
print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>";
print "Stack trace is ".$e->getTraceAsString();
}
}

关于php - 使用 Youtube API V3 和 PHP 将视频上传到 Youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14236502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com