I'm developing an asp.net core (1.0 LTS) application for a company. It dynamically generates reports using mainly Google Analytics, but also other APIs, and currently uses a self-signed SSL certificate.
I have a custom Oauth2 authorization flow which authorizes with Google server-side using the normal analytics.readonly scope, and then the user ends up calling on another method which accesses the analytics/v3/data/ga endpoint. The access token is sent in the authorization header.
This is my problem: I keep getting 403 forbidden when POSTing to the v4 batchGet endpoint instead. My POST code looks like this:
HttpResponseMessage response = await client.PostAsync("https://analyticsreporting.googleapis.com/v4/reports:batchGet",
new StringContent(requestBody));
Where requestBody is a string containing JSON, which looks like this:
{
"reportRequests": [
{
"viewId": "XXXX",
"dateRanges": [
{
"startDate": "2017-06-01",
"endDate": "2017-06-30"
}
],
"metrics": [
{
"expression": "ga:sessions"
}
]
}
]
}
Since I'm getting a 403, I'm assuming the authorization is valid. If I POST without it I get the expected 401 error. The logical conclusion seems to be that my Analytics account doesn't have sufficient permissions to access the data, but I can get the exact same data using either the Request Composer, or the API explorer on this page when building the same JSON. In all cases I'm using the same account and the same viewId.
Further questions I have besides "why isn't this working":
Is it possible to access the v4 endpoint this way, or do I have to use their Client Library?
Are the Google Docs really up-to-date?
Any input / experience is much appreciated, thanks.
Okay, so this is ridiculous, but in the interest of clarity I'll explain what went wrong and how this was resolved. Apparently, the Google Reporting API wasn't enabled.
Right, I know. In Google's list of APIs, the Reporting API isn't listed by default. It just lists popular APIs, and with any careful reading, it becomes clear that you have to search to get the right API.
Maybe I thought "of course the Reporting API is listed under popular, so it has to be the one called Analytics API" or maybe I missed the qualification "popular" altogether. Either way, it's now working like a charm and I didn't have to change any of my original code.
I think there's something to say about how Google communicates their information in general, but that's for another time. Let's just say it took quite a while before I accidentally found the Reporting API page where I saw it wasn't enabled. Well that was embarrassing!