【问题标题】:how to use Youtube API V3 to get Processing Details in PHP如何使用 Youtube API V3 在 PHP 中获取处理细节
【发布时间】:2015-06-28 06:37:21
【问题描述】:

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

当我使用下面的参数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");

关于处理来自 Google 的 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());
    }
  }
}

【问题讨论】:

  • 通过 oAuth 进行身份验证的用户是否拥有您要获取其详细信息的视频?我假设是这样,但确定一下会很有帮助。

标签: php json oauth youtube youtube-api


【解决方案1】:

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

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

我不知道你为什么会收到 403 禁止。当我使用 GET https://www.googleapis.com/youtube/v3/videos?part=processingDetails&id=eny3OJsuosE&key={YOUR_API_KEY} 在 https://developers.google.com/youtube/v3/docs/videos/list 尝试它时,我收到了

{
  "kind": "youtube#videoListResponse", 
  "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/1ufoYPsPME45Z4CJ7jyRlwoAAwQ\"",
  "pageInfo": {
    "totalResults": 0,
    "resultsPerPage": 0
  },
  "items": [
  ]
}

【讨论】:

    猜你喜欢
    • 2014-10-10
    • 2016-10-22
    • 2014-01-26
    • 2015-03-14
    • 2017-09-17
    • 2017-07-25
    • 2016-04-14
    • 1970-01-01
    • 2017-12-06
    相关资源
    最近更新 更多