【问题标题】:Youtube Analytics API PHP Invalid query. Query did not conform to the expectationsYoutube 分析 API PHP 查询无效。查询不符合预期
【发布时间】:2014-11-03 12:29:53
【问题描述】:

我正在尝试从服务器到服务器进行一些分析查询。 我将 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 Explorer 中执行相同的查询: https://developers.google.com/apis-explorer/#p/youtubeAnalytics/v1/youtubeAnalytics.reports.query?ids=channel%253D%253DMINE&start-date=2014-05-01&end-date=2014-06-30&metrics=views%252CestimatedMinutesWatched&dimensions=day&sort=day%252C-views&_h=1&

有什么想法吗?

【问题讨论】:

  • 嗨,我想我也遇到了同样的问题!您如何将其修改为 oAuth2?

标签: php youtube google-analytics google-api-php-client youtube-analytics


【解决方案1】:

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

【讨论】:

  • 嗯,这可能就是问题所在,但我在分析文档中找不到任何说明这一点的内容。无论如何,这很可惜,我可以将它用于其他类型的 api,但不能用于 youtube...无论如何,我会将其更改为 oAuth2 并尝试使用它。
  • 这就是问题所在,我将它修改为 oAuth2,它的工作原理就像一个魅力,可惜互联网上没有这方面的教程。当我有更多时间时,我会写下我的经验以及如何设置它。
  • 在我链接的那个页面中,如果您搜索“服务帐户”,您会发现:“YouTube 不支持服务帐户,如果您尝试使用服务帐户进行身份验证,您将获得这个错误。'
  • 没错,但您链接的是 Youtube Data API 而不是 Youtube Analytics API 文档 :)
  • 但是,它也在分析中指出,我没有给予足够的关注:服务帐户流支持不访问用户信息的服务器到服务器交互。但是,YouTube 分析 API 不支持此流程。由于无法将服务帐户链接到 YouTube 帐户,因此尝试使用此流程授权请求将产生错误。感谢您的帮助:)
【解决方案2】:

您需要添加一个 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2011-08-11
    • 2012-12-30
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多