所有通过Google PHP库对Youtube Analytics API的请求导致400个错误请求


All Requests to Youtube Analytics API via Google PHP Library Results in 400 Bad Request

我能够通过API资源管理器成功向Youtube Analytics API发出请求。我的代码试图使用GooglePHP客户端库,特别是Google_Service_YouTubeAnalytics类。不幸的是,没有关于这个类的文档。

我正在客户端上设置ID和断言凭据。我很有信心这是正确的工作,因为如果我将私钥更改为我知道不正确的东西,我会得到:

{"code":400,"error":"Error refreshing the OAuth2 token, message: '{'n '"error'" : '"invalid_grant'"'n}'"}

但当我插入正确的私钥时,我会得到以下响应:

{"code":400,"error":"Error calling GET https:'/'/www.googleapis.com'/youtube'/analytics'/v1'/reports?ids=channel%3D%3DCHANNEL_ID&start-date=2014-09-01&end-date=2014-09-05&metrics=views%2Cuniques: (400) Invalid query. Query did not conform to the expectations."}

它不会告诉我查询中什么是无效的(这将非常有帮助),所以我不知道我可能做错了什么。感谢您的帮助。

这是我提出请求的代码:

$client = new 'Google_Client();
$client->setApplicationName(self::APP_NAME);
// set some stuff
$client->setClientId( self::CLIENT_ID );
$client->setClientSecret( self::CLIENT_SECRET );
$client->setAssertionCredentials(new 'Google_Auth_AssertionCredentials(
    self::CRED_ID,
    [
        "https://www.googleapis.com/auth/youtube.readonly",
        'https://www.googleapis.com/auth/yt-analytics.readonly'
    ],
    self::youtubeKey()
));
$youtubeService = new 'Google_Service_YouTubeAnalytics($client);
$resp = $youtubeService->reports->query(
    self::CHANNEL_ID,
    '2014-09-01',
    '2014-09-05',
    'views,uniques'
);

您正在进行一个不受支持的查询,不可能使用views&uniques没有提供尺寸。您可以在Youtube的分析API参考中查看。

试着添加一个类似的维度,它就会起作用:

$youtubeService = new 'Google_Service_YouTubeAnalytics($client);
$resp = $youtubeService->reports->query(
    self::CHANNEL_ID,
    '2014-09-01',
    '2014-09-05',
    'views,uniques',
    array('dimensions' => 'day')
);

该查询将得到类似于的响应

200 OK
- Show headers -
{
 "kind": "youtubeAnalytics#resultTable",
 "columnHeaders": [
  {
   "name": "day",
   "columnType": "DIMENSION",
   "dataType": "STRING"
  },
  {
   "name": "views",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  },
  {
   "name": "uniques",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  }
 ],
 "rows": [
  [
   "2014-09-04",
   1250,
   621
  ],
  [
   "2014-09-05",
   1265,
   577
  ],
  [
   "2014-09-03",
   1255,
   557
  ],
  [
   "2014-09-01",
   1076,
   532
  ],
  [
   "2014-09-02",
   1182,
   570
  ]
 ]
}

谷歌的API资源管理器是一个非常有用的工具,可以测试您的查询。


出于文档的目的,您可以查看源代码和类本身,它们都有很好的文档记录,而且"可能"是不言自明的。

  • YouTubeAnalytics.php
  • 客户端.php

一种较新的方法是使用oAuth 2.0协议向此API发出请求以授权访问
谷歌提供了一个惊人的资源来尝试所有这些东西:OAuth 2.0 Playground

基本上,您需要获得一个访问令牌及其刷新令牌,以便在之前的令牌过期时应用它。

$client = new 'Google_Client();
$client->setApplicationName(self::APP_NAME);
// set some stuff
$client->setClientId( self::CLIENT_ID );
$client->setClientSecret( self::CLIENT_SECRET );
// Set oAuth info
$client->addScope('Google_Service_YouTubeAnalytics::YT_ANALYTICS_READONLY);
$client->setAccessToken($accessToken);
// Check if token is expired
if ($client->isAccessTokenExpired()) {
    $client->refreshToken($refreshToken());
    $newToken = $client->getAccessToken();
    $authObj = json_decode($newToken);
    if (!is_object($authObj)) {
        throw new 'Google_Auth_Exception('Error on updating oAuth tokens');
    }
    //update tokens
    //...            
}
$youtubeService = new 'Google_Service_YouTubeAnalytics($client);

希望它能有所帮助!

您需要添加一个Google API密钥

    https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel_ID&start-date=2014-09-01&end-
date=2014-10-01&metrics=views%2Cuniques&key={YOUR_API_KEY}

此外,我不确定"%2Uniques"=>"uniques"是否是有效的度量。

你可以使用谷歌自动工具生成一个有效的链接。

https://developers.google.com/youtube/analytics/v1/