dpik71879 2015-04-21 08:11
浏览 56

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

I have authorization and want to get the video's processing status.

When I'm use below parameter part=status, everthing was fine.

$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 return a json object to me:

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
                    )

            )

    )

)

When I'm switch to processingDetails instead of status.

$res = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=$videoId&key=$apiKey&part=processingDetails");

Nothing about processing json data return from Google.

stdClass Object
(
    [error] => stdClass Object
        (
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [domain] => youtube.common
                            [reason] => forbidden
                            [message] => Forbidden
                        )

                )

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

)

Is it my authentication got problem?

Below is my .php file (I set my server api key, client oauth correctly, I temporary change to sample key for this question):

<?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条回答 默认 最新

  • dongmubi4444 2015-04-21 14:01
    关注

    processingDetails are for when the video is still processing on YouTube. After that's done, there are no processingDetails. While it IS happening, the data return would be

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

    I don't know why you received a 403 forbidden though. When I tried it at https://developers.google.com/youtube/v3/docs/videos/list with GET https://www.googleapis.com/youtube/v3/videos?part=processingDetails&id=eny3OJsuosE&key={YOUR_API_KEY} I received

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

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上