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'
);
You are making a not supported query, it's no possible to use views & uniques with no dimensions provided. You can check it in Youtube's Analytics API Reference.
Try add it a dimension like day and it will work:
$youtubeService = new \Google_Service_YouTubeAnalytics($client);
$resp = $youtubeService->reports->query(
self::CHANNEL_ID,
'2014-09-01',
'2014-09-05',
'views,uniques',
array('dimensions' => 'day')
);
That query will get a Response similar to:
200 OK
- Show headers -
{
"kind": "youtubeAnalytics#resultTable",
"columnHeaders": [
{
"name": "day",
"columnType": "DIMENSION",
"dataType": "STRING"
},
{
"name": "views",
"columnType": "METRIC",
"dataType": "INTEGER"
},
{
"name": "uniques",
"columnType": "METRIC",
"dataType": "INTEGER"
}
],
"rows": [
[
"2014-09-04",
1250,
621
],
[
"2014-09-05",
1265,
577
],
[
"2014-09-03",
1255,
557
],
[
"2014-09-01",
1076,
532
],
[
"2014-09-02",
1182,
570
]
]
}
Google's APIs Explorer is a very useful tool in order to test your queries.
For documentation purposes you can have a look to the source code and the classes themselves, they are very well documented and "maybe" self-explanatory.
A newer approach is to make requests to this API using oAuth 2.0 protocol for authorizing access.
Google provides an amazing resource to try all this stuff: OAuth 2.0 Playground
Basically, you need to get an access token and its refresh token to apply it when the prior expires.
$client = new \Google_Client();
$client->setApplicationName(self::APP_NAME);
// set some stuff
$client->setClientId( self::CLIENT_ID );
$client->setClientSecret( self::CLIENT_SECRET );
// Set oAuth info
$client->addScope(\Google_Service_YouTubeAnalytics::YT_ANALYTICS_READONLY);
$client->setAccessToken($accessToken);
// Check if token is expired
if ($client->isAccessTokenExpired()) {
$client->refreshToken($refreshToken());
$newToken = $client->getAccessToken();
$authObj = json_decode($newToken);
if (!is_object($authObj)) {
throw new \Google_Auth_Exception('Error on updating oAuth tokens');
}
//update tokens
//...
}
$youtubeService = new \Google_Service_YouTubeAnalytics($client);
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With