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

通过Google PHP库对Youtube Analytics API的所有请求都会导致400个错误请求,php,youtube-api,google-api-php-client,youtube-analytics,Php,Youtube Api,Google Api Php Client,Youtube Analytics,我能够通过API浏览器成功地向Youtube Analytics API发出请求。我的代码试图使用Google PHP客户端库,特别是Google_服务_YouTubeAnalytics类。不幸的是,没有关于这个类的文档 我正在客户端上设置ID和断言凭据。我确信这是正确的,因为如果我将私钥更改为我知道不正确的内容,我会得到: {“code”:400,“error”:“刷新OAuth2令牌时出错,消息:'{\n\'error\':\'invalid\'u grant\'\n}'} 但是,当我插入正

我能够通过API浏览器成功地向Youtube Analytics API发出请求。我的代码试图使用Google PHP客户端库,特别是Google_服务_YouTubeAnalytics类。不幸的是,没有关于这个类的文档

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

{“code”:400,“error”:“刷新OAuth2令牌时出错,消息:'{\n\'error\':\'invalid\'u grant\'\n}'}

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

{“code”:400,“error”:“调用GET https:\/\/www.googleapis.com\/youtube\/analytics\/v1\/reports时出错?ids=channel%3D%3DCHANNEL\u ID&start date=2014-09-01&end date=2014-09-05&metrics=views%2Cuniques:(400)查询无效。查询不符合预期。”

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

以下是我提出请求的代码:

$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'
);

您需要添加一个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}
此外,我不确定“%2Cuniques”=>“uniques”是否为有效度量

您可以使用谷歌自动工具生成有效链接


您正在进行不受支持的查询,无法使用未提供维度的视图和唯一性。 你可以登记

尝试将其添加为day这样的维度,它将起作用:

$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
  ]
 ]
}
是测试查询的非常有用的工具


出于文档的目的,您可以查看源代码和类本身,它们都有很好的文档,并且“可能”是自解释的


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

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

$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 Analytics客户端库,效果很好。我几乎以同样的方式使用代码。Uggh.你的应用程序是否真的发送字符串“channel==channel_ID”作为ids参数的值,或者你粘贴错误代码时所做的修改是否如此?不,我更改了它是为了在本文中隐藏我的实际频道ID。在我的代码中,它是我的通道ID值。发布您用于发出引发此错误的请求的代码。另外,阅读关于库源代码的评论通常是非常有帮助的。有关于这个问题的更新吗?我面临同样的问题,但我尝试使用服务帐户,而不是oAuth。