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

时间:2015-04-21 08:11:30

标签: php json oauth youtube youtube-api

我已获得授权,希望获得视频的处理状态。

当我在参数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);

Google将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());
    }
  }
}

1 个答案:

答案 0 :(得分:0)

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