1

我正在尝试从服务器到服务器进行一些分析查询。我将 laravel 与https://github.com/google/google-api-php-client库一起使用。

这是我正在使用的代码:

    $client = new Google_Client();
$key = file_get_contents(storage_path('key.p12'));
$cred = new Google_Auth_AssertionCredentials(
        '***@developer.gserviceaccount.com', array('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly'), $key);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

Session::put('service_token', $client->getAccessToken());


  $service = new Google_Service_YouTubeAnalytics($client);
  $id = 'channel==MINE';
  $start_date = '2014-05-01';
  $end_date = '2014-06-30';

  $optparams = array(
  'dimensions' => 'day',
  'sort' => 'day,-views'
  );

  $metric = 'views,estimatedMinutesWatched';

  $api = $service->reports->query($id, $start_date, $end_date, $metric, $optparams);

  $service = new Google_Service_YouTubeAnalytics($client);
  $id = 'channel==MINE';
  $start_date = '2014-05-01';
  $end_date = '2014-06-30';

  $optparams = array(
  'dimensions' => 'day',
  'sort' => 'day,-views'
  );

  $metric = 'views,comments,favoritesAdded,likes,dislikes,estimatedMinutesWatched,averageViewDuration';//'views,estimatedMinutesWatched';

  $api = $service->reports->query($id, $start_date, $end_date, $metric, $optparams);

我的问题是身份验证工作正常,但我无法让分析查询开始工作,我收到以下错误:

Error calling GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2014-05-01&end-date=2014-06-30&metrics=views%2CestimatedMinutesWatched&dimensions=day&sort=day%2C-views: (400) Invalid query. Query did not conform to the expectations. 

即使在 API 资源管理器中执行相同的查询: https ://developers.google.com/apis-explorer/#p/youtubeAnalytics/v1/youtubeAnalytics.reports.query?ids=channel%253D%253DMINE&start-date= 2014-05-01&结束日期=2014-06-30&metrics=views%252CestimatedMinutesWatched&dimensions=day&sort=day%252C-views&_h=1&

任何想法?

4

2 回答 2

3

我测试了您的查询,它运行良好,但显然 YouTube 不支持服务帐户。文档说在这种情况下它应该返回 a 403,但由于某种原因它正在返回400.

于 2014-11-03T15:29:26.310 回答
0

您需要添加一个 Google API 密钥

https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2014-05-01&end-date=2014-06-30&metrics=views%2CestimatedMinutesWatched&dimensions=day&sort=day%2Cviews&key={YOUR_API_KEY}

此外,如果您查看字符串,最后会出现类型错误,其中“-views”应该只是没有破折号的“views”。您可以使用 Google 自动化工具生成有效链接。

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

于 2014-12-09T17:27:20.733 回答