【问题标题】:Getting 'Forbidden error' when trying to execute youtube analytic API尝试执行 youtube 分析 API 时出现“禁止错误”
【发布时间】:2017-01-23 03:16:17
【问题描述】:

我在尝试执行 youtube 分析 API 时收到一条错误消息。

发生服务错误:调用 GET 时出错 https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DUCaayLD9i5x4MmIoVZxXSv_g&start-date=2016-08-01&end-date=2016-08-07&metrics=views&dimensions=7DayTotals: (403) 禁止

这是我的代码:

require_once __DIR__.'\google-api-php-client-1-master\src\Google\autoload.php';
require_once __DIR__.'\google-api-php-client-1-master\src\Google\Client.php';
require_once __DIR__.'\google-api-php-client-1-master\src\Google\Service\YouTube.php';
session_start();
$key = file_get_contents('mykey.json');
$OAUTH2_CLIENT_ID = 'xx-xx-xx';
$OAUTH2_CLIENT_SECRET = 'xxx';
$scope = array("https://www.googleapis.com/auth/youtube.force-ssl", "https://www.googleapis.com/auth/youtubepartner-channel-audit", "https://www.googleapis.com/auth/youtube", "https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/yt-analytics.readonly", "https://www.googleapis.com/auth/yt-analytics-monetary.readonly","https://www.googleapis.com/auth/youtubepartner");

    $client = new Google_Client();
    $client->setClientId($OAUTH2_CLIENT_ID);
    $client->setClientSecret($OAUTH2_CLIENT_SECRET);
    $client->setAccessType('offline');
    $client->setAccessToken($key);
    $client->setScopes($scope);
      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()) {
            //refresh your access token if it's expired
            $newToken = json_decode($client->getAccessToken());
            $client->refreshToken($newToken->refresh_token);
            file_put_contents('mykey.json', $client->getAccessToken());
        }

        $analytics = new Google_Service_YouTubeAnalytics($client);
            $channel_url = 'UCaayLD9i5x4MmIoVZxXSv_g';
            $ids = 'channel==' . $channel_url . '';
            $end_date = '2016-08-07';
            $start_date = '2016-08-01';
            $optparams = array(
            'dimensions' => '7DayTotals',
            );
            $metric = 'views';
            $api = $analytics->reports->query($ids, $start_date, $end_date, $metric, $optparams);
            echo '<pre>';print_r($api);die;
  }

我已经启用了'youtube analytic API',并且我从here获得了access_token

我的代码有什么问题,或者我们需要做更多的事情来摆脱它吗?

【问题讨论】:

  • 谁能建议我如何解决这个问题。
  • 嘿@Rahul,你在哪里提到你想要获取分析的视频ID,在你的代码中?您的代码中缺少它。
  • 我认为不需要视频 ID,我只想获得特定频道的 30 天或 7 天的总观看次数。
  • 这和权限有关。
  • 您有自己的 youtube 频道吗?请改用该频道 ID,并尝试在 API Explorer 中执行

标签: php youtube-analytics-api


【解决方案1】:

查看您的代码,您似乎在将其保存到文件时正在生成一个新的访问令牌(使第一个无效)。

试试:

  if($client->isAccessTokenExpired()) {
        //refresh your access token if it's expired
        $newToken = json_decode($client->getAccessToken());
        $client->refreshToken($newToken->refresh_token);
        file_put_contents('mykey.json', $newToken);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 2019-08-07
    • 2020-02-17
    • 1970-01-01
    相关资源
    最近更新 更多