1

I'm trying to make some analytics query from server to server. I'm using laravel with https://github.com/google/google-api-php-client library.

This is the code I'm using:

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

My problem is that the authentication is working just fine, but I can't get the analytics query get to work, I'm getting the following error:

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. 

Even though the same exact query is working in the 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&

Any idea?

G.T.
  • 562
  • 8
  • 18

2 Answers2

3

I tested your query and it's working fine, but apparently YouTube does not support service accounts. The documentation says it should return a 403 in this case, but for some reason it's returning 400.

Vinicius Braz Pinto
  • 8,209
  • 3
  • 42
  • 60
  • Well probably that will be the problem, but I couldn't find anything stating this in the analytics documentation. It's a pitty anyway, that I can use it for other type of api, but not youtube... Anyway I'll change it to oAuth2 then and try it with that. – G.T. Nov 04 '14 at 07:00
  • 2
    That was the problem, I modified it to oAuth2 and it works like a charm, it's a pity that there is no tutorial for this on the internet. When I'll have more time I'll write down my experience and how to set it up. – G.T. Nov 04 '14 at 07:20
  • in that page I linked, if you search for 'service account' you'll find this: 'YouTube does not support Service Accounts, and if you attempt to authenticate using a Service Account, you will get this error.' – Vinicius Braz Pinto Nov 04 '14 at 14:08
  • 1
    That's true, but you linked Youtube Data API and not Youtube Analytics API documentation :) – G.T. Nov 05 '14 at 08:45
  • But yeah, it states on the Analytics also, I wasn't paying enough attention: The service account flow supports server-to-server interactions that do not access user information. However, the YouTube Analytics API does not support this flow. Since there is no way to link a Service Account to a YouTube account, attempts to authorize requests with this flow will generate an error. Thanks for the help :) – G.T. Nov 05 '14 at 08:47
0

You need to add a Google API key

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}

also if you look at your string you have type error on the end where "-views" should be just "views" without dash You can use Google automated tool to generate a valid link.

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

SergeDirect
  • 2,019
  • 15
  • 20