I am using Web API 2 to create a Google Analytics Reporting API v4 proxy. I am successfully creating the request parameters using the Google.Apis.AnalyticsReporting.v4
library and succesfully authenticating with a service account. However, when running the BatchGet.Execute()
method, the request for batch reports fail.
Fiddler shows the request is a 404:
The requested URL /v4/reports%3AbatchGet
was not found on this server. That’s all we know.
Using Postman I can see that the if the URL was correct (https://analyticsreporting.googleapis.com/v4/reports:batchGet
) the request succeeds. Something on the back-end of Web API or .NET 4.6.1 is encoding the request URL and causing Google to send back a 404.
How can I configure Web API 2 so that it allows the colon within the URL for the Google API request?
public IHttpActionResult Post([FromBody] RequestObject req)
{
var resp = new CompleteResponse();
try
{
var service = new AnalyticsReportingService(new BaseClientService.Initializer
{
HttpClientInitializer = Cert,
ApplicationName = "GA Test"
});
var report = new GetReportsRequest();
var result = service.Reports.BatchGet(report).Execute();
resp.Result = result.Reports[0].Data;
}
catch (Exception ex)
{
resp.Error = ex.ToString();
}
return Ok(resp);
}
Global.asax:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.RequestHeaderMapping("Accept",
"text/html",
StringComparison.InvariantCultureIgnoreCase,
true,
"application/json"));
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
There is a known bug that was fixed in 1.31.1. The solution was updating to the latest version.
See https://github.com/google/google-api-dotnet-client/pull/1121