Getting 400 Bad Request with Analytics Batch Request
See original GitHub issueHi. I’m having a problem with a Batch Request that I am doing. It was previously working fine a couple weeks ago, I left it for a week, came back to it and now it is giving 400 Bad Requests even when only one request is queued. It errors on await request.ExecuteAsync()
with the message Response status code does not indicate success: 400 (Bad Request)
. For authorisation I am using a service account. Here is the code:
public async Task<List<RealtimeData>> GetRealTimeAnalyticsAsync(IEnumerable<string> tableIDs, string dimensions)
{
List<RealtimeData> realTimeData = new List<RealtimeData>();
List<RequestError> requestErrors = new List<RequestError>();
var request = new BatchRequest(_analyticsService);
foreach (var tableId in tableIDs)
{
var req = _analyticsService.Data.Realtime.Get($"ga:{tableId}", "rt:activeUsers");
req.Dimensions = dimensions;
request.Queue<RealtimeData>(req, (content, errors, i, message) =>
{
if (content != null) realTimeData.Add(content);
if (errors != null) requestErrors.Add(errors);
});
}
await request.ExecuteAsync();
return realTimeData;
}
I know Google have been updating their Batch Request endpoints, deprecating the global endpoint. Having followed the .NET instructions on this link. I can confirm all my Google Client Libraries are up to date and from debugging the code the BatchURI and BatchURL have values https://www.googleapis.com/batch/analytics/v3
.
I’m not really sure what I’m missing or what I may have done to break it. I’ve had some conversations with the Google Analytics API support team and with their help I was able to make curl requests to https://www.googleapis.com/batch/analytics/v3
fine. Also my code works fine when making single requests and not batching them.
Thanks for any help or guidance available!
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (8 by maintainers)
Top GitHub Comments
Happy that is working for you as well!
The problem was related to a change in service config that made Analytics batch requests requiere Content-ID to be set per each individual request. I din’t mentioned it before, sorry for that, but if you would have examined the body of the 400 response, you would have found a JSON which ultimately contained an error message saying something like “Content-ID missing for each individual request”. For the Analytics API, Content-ID is optional, the config has now been fixed to that purpose. Again, thanks for reporting!
(Do create a new issue next time 😃). But, I think you’d want to wait 1 second after you have received the response from the previous batch, you are now waiting 1 second in between batch starts but that means that batches might be even executing at the same time. Something like this:
I would advise you though to also ask about this on the Analytics API support channels, we are not experts on each individual API, just the client libraries, and they might have better alternatives.
I’ll close this issue now, since I believe we have addressed your problem, but don’t hesitate to create new ones if you encounter something else.
@WarrenH97 @erikosterholm83 The fix should now be live. My code that failed before is now working as expected. I’ll leave this issue open for a couple of days until you can confirm.