gpt4 book ai didi

php - 如何使用 Youtube API V3 在 PHP 中获取处理细节

转载 作者:行者123 更新时间:2023-12-03 05:39:32 26 4
gpt4 key购买 nike

我有授权,想获取视频的处理状态。

当我使用下面的参数 part=status ,一切都很好。

$res = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=$videoId&key=$apiKey&part=status");
$res = json_decode($res);
echo '<pre>';
print_r($res);

谷歌返回一个 json 对象给我:
stdClass Object
(
[kind] => youtube#videoListResponse
[etag] => "IHLB7Mi__JPvvG2zLQWAg8l36UU/jHUy0MMNhdmjXSf-2G16DKw_k8s"
[pageInfo] => stdClass Object
(
[totalResults] => 1
[resultsPerPage] => 1
)

[items] => Array
(
[0] => stdClass Object
(
[kind] => youtube#video
[etag] => "IHLB7Mi__JPvvG2zLQWAg8l36UU/XOUFG4OJCu-VDZTrtjJ4NHmYjEk"
[id] => eny3OJsuosE
[status] => stdClass Object
(
[uploadStatus] => uploaded
[privacyStatus] => unlisted
[license] => youtube
[embeddable] => 1
[publicStatsViewable] => 1
)

)

)

)

当我切换到 processingDetails而不是状态。
$res = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=$videoId&key=$apiKey&part=processingDetails");

没有关于处理从谷歌返回的 json 数据。
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => youtube.common
[reason] => forbidden
[message] => Forbidden
)

)

[code] => 403
[message] => Forbidden
)

)

是我的身份验证有问题吗?

下面是我的 .php 文件(我正确设置了我的服务器 api key 、客户端 oauth,我临时更改为这个问题的示例 key ):
<?php
/*
* Grab offline Key
*/
$key = file_get_contents('the_key.txt');
require_once ('lib/Google/autoload.php');


$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$apiKey = "mygoogleapiKey-serverkey"; // Change to your API key.

// Warn if the API key isn't changed!
if (strpos($apiKey, "<") !== false)
{
echo missingApiKeyWarning();
exit;
}
else
{

$client->setClientId('clientID-clientID.apps.googleusercontent.com');
$client->setClientSecret('clientSecret-clientSecret');
$client->setScopes('https://www.googleapis.com/auth/youtube');

//Init Auth Login Key
$client->setAccessType('offline');
$client->setAccessToken($key);

$client->setDeveloperKey($apiKey);

if ($client->getAccessToken())
{

/**
* Check to see if our access token has expired. If so, get a new one and save it to file for future use.
*/
if($client->isAccessTokenExpired()) {
echo 'Token Expired';
echo '<hr/>';
$newToken = json_decode($client->getAccessToken());
$client->refreshToken($newToken->refresh_token);
file_put_contents('the_key.txt', $client->getAccessToken());
}


$youtube = new Google_Service_YouTube($client);

/************************************************
To actually make the batch call we need to
enable batching on the client - this will apply
globally until we set it to false. This causes
call to the service methods to return the query
rather than immediately executing.
************************************************/
$client->setUseBatch(true);

try
{
$videoId = 'eny3OJsuosE';
stream_context_set_default(['http' => ['ignore_errors' => true]]);
$res = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=$videoId&key=$apiKey&part=processingDetails");
$res = json_decode($res);
echo '<pre>';
print_r($res);
}
catch (Google_ServiceException $e)
{
//Google_ServiceException
echo '<p>A service error occurred: </p>';
echo htmlspecialchars($e->getMessage());
}
catch (Google_Exception $e)
{
//Google_Exception
echo '<p>A service error occurred: </p>';
echo htmlspecialchars($e->getMessage());
}
}
}

最佳答案

processingDetails 适用于视频仍在 YouTube 上处理的时间。完成后,就没有 processingDetails 了。当它发生时,数据返回将是

"processingDetails": {
"processingStatus": string,
"processingProgress": {
"partsTotal": unsigned long,
"partsProcessed": unsigned long,
"timeLeftMs": unsigned long
},

我不知道你为什么收到 403 禁止。当我在 https://developers.google.com/youtube/v3/docs/videos/list 尝试时使用 GET https://www.googleapis.com/youtube/v3/videos?part=processingDetails&id=eny3OJsuosE&key= {YOUR_API_KEY} 我收到了
{
"kind": "youtube#videoListResponse",
"etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/1ufoYPsPME45Z4CJ7jyRlwoAAwQ\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 0
},
"items": [
]
}

关于php - 如何使用 Youtube API V3 在 PHP 中获取处理细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766477/

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