I'm trying to fetch a very simple data set from a google analytics report and haven't gotten it to authenticate. I'm really lost. I've been using the APIs explorer to test what I'm trying to do out and can't seem to do a simple batch get. I've blanked out the key field (obv)
This is basically what I'm trying to get to send back: Services > Google Analytics Reporting API v4 > analyticsreporting.reports.batchGet
Note: I'm not trying to do OAuth2 but just fire this stuff with a server API key.
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet?fields=reports&key=XXX
{
"reportRequests": [
{
"dateRanges": [
{
"startDate": "2014-06-23",
"endDate": "2016-06-23"
}
],
"orderBys": [
{
"fieldName": "ga:pageviews",
"sortOrder": "DESCENDING"
}
],
"dimensions": [
{
"name": "ua:page"
}
]
},
{
"viewId": "7070072"
}
]
}
This is what comes back:
{
"error": {
"code": 401,
"message": "The request does not have valid authentication credentials.",
"status": "UNAUTHENTICATED"
}
}
API keys are for un-authenticated API's like Google Analytics Metadata API. The Analytics Reporting API V4 requires a valid OAuth token. Think about it this way, would you want just anyone with an API key to access your GA data? The API needs to verify that the user has the authority to access the data. Please read through the various OAuth Scenarios and find the one that matches your use case.
So it sounds like you want to authenticate with a service account. The issue is not with your analytics code but how you are authenticating. In order to make the request you need to have a valid OAuth token. Since you specifically listed that you are interested in .NET you should look at the Google APIs .NET client library documentation. Remember that you will need to add the service account email to the Google Analytics View you wish to access.