Youtube Analytics API PHP Invalid request. The request did not meet expectations

I am trying to make some analytic queries from server to server. I am using laravel with the 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 authentication works fine, but I cannot get the analytics request to work, I get 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. 

Despite the fact that the same exact request works in the API browser: 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?

+1
source share
2 answers

I tested your request and it works fine, but YouTube doesn't seem to support service accounts. The documentation says that in this case it should return 403 , but for some reason it returns 400 .

+3
source

You need to add 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 line, you have a type error at the end where "-views" should just be "views" without dashes. You can use the Google tool to create the correct link.

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

0
source

Source: https://habr.com/ru/post/976882/


All Articles