I am able to successfully make requests to Youtube Analytics API via the API Explorer. My code is attempting to use the Google PHP Client library, specifically the Google_Service_YouTubeAnalytics class. Unfortunately, there is no documentation on this class.
I am setting the ID and Assertion Credentials on the client. I'm fairly confident this is working correctly, because if I change the private key to something I know to be incorrect, I get:
{"code":400,"error":"Error refreshing the OAuth2 token, message: '{\n \"error\" : \"invalid_grant\"\n}'"}
But when I insert the correct private key, I get the following response:
{"code":400,"error":"Error calling GET https:\/\/www.googleapis.com\/youtube\/analytics\/v1\/reports?ids=channel%3D%3DCHANNEL_ID&start-date=2014-09-01&end-date=2014-09-05&metrics=views%2Cuniques: (400) Invalid query. Query did not conform to the expectations."}
It doesn't tell me what is invalid about the query (which would be incredibly helpful), so I have no idea what I could be doing incorrectly. Any help is appreciated.
Here is my code that makes the request:
$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'
);