如何使用 Youtube API V3 获取 PHP 中的处理详细信息



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

当我在参数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 密钥、客户端 oauth,我临时更改为此问题的示例密钥):

<?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": {
"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": [
  ]
}

相关内容

  • 没有找到相关文章

最新更新