Search code examples
c#google-analyticsgoogle-apihttp-status-code-400

400 Bad Request using Google c# API


I am receiving a 400 bad request error when running the c# google api. I know its ultimately because the API builds a querystring and the length of the query string is to long. How do I force the API to POST instead of GET?

My code:

        string username = "SAMPLE@SAMPLE.COM";
        string pass = "PASS";
        string gkey = "?key=XXXXXXXXXXXXXXXXXXXXXXXXXXX";

        string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;
        string accountFeedUrl = "https://www.googleapis.com/analytics/v2.4/management/accounts" + gkey;

        AnalyticsService service = new AnalyticsService("API Sample");
        service.setUserCredentials(username, pass);

        DataQuery query1 = new DataQuery(dataFeedUrl);


        query1.Ids = "ga:34197921";
        query1.Dimensions = "ga:medium,ga:campaign,ga:date,ga:isMobile,ga:isTablet,ga:searchKeyword,ga:hostname";
        query1.Metrics = "ga:visits,ga:visitors,ga:visitBounceRate,ga:goalStartsAll,ga:goalCompletionsAll,ga:goal1Starts,ga:goal1Completions,ga:goal2Starts,ga:goal2Completions,ga:goal3Starts,ga:goal3Completions,ga:goal4Starts,ga:goal4Completions,ga:adCost,ga:totalValue";
        query1.Sort = "ga:date,ga:hour";
        query1.NumberToRetrieve = 50;


        query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd");
        query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
        query1.StartIndex = 1;

My exception:

Google.GData.Client.GDataRequestException: Execution of request failed: https://www.google.com/analytics/feeds/data?key=XXXXXXXX&start-index=1&max-results=10000&dimensions=ga:medium,ga:campaign,ga:date,ga:isMobile,ga:isTablet,ga:searchKeyword,ga:hostname&end-date=2013-11-15&ids=ga:34892951&metrics=ga:visits,ga:visitors,ga:visitBounceRate,ga:goalStartsAll,ga:goalCompletionsAll,ga:goal1Starts,ga:goal1Completions,ga:goal2Starts,ga:goal2Completions,ga:goal3Starts,ga:goal3Completions,ga:goal4Starts,ga:goal4Completions,ga:adCost,ga:totalValue&start-date=2012-01-02 ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse() at Google.GData.Client.GDataRequest.Execute() --- End of inner exception stack trace --- at Google.GData.Client.GDataRequest.Execute() at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) at Google.GData.Client.GDataGAuthRequest.Execute() at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince,String etag, Int64& contentLength) at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince) at Google.GData.Client.Service.Query(FeedQuery feedQuery) at Google.GData.Analytics.AnalyticsService.Query(DataQuery feedQuery)

Solution

  • There is no POST available for the Core Reporting API. However this isn't the problem, you have some other issues.

    1. You should move to use the Core Reporting API v2.4 since the dataFeedUrl is pointing to the an older v2.3, which redirects anyway and returns a v2.4 response. So you might as well just use the v2.4 endpoint for the dataFeedurl. https://www.googleapis.com/analytics/v2.4/data
    2. A single query can have at most 7 dimensions and 10 metrics. You have more than 10 metrics so you're going to get a 400 Bad Request. There should be an accompanying error message with the 400 to tell you the problem. It doesn't look like that is exposed in your response, but it's probably telling you too many metrics.
    3. Finally, just a recommendation: Move to v3 which returns JSON and has some new features (v2.4 doesn't get updated anymore). And client login is deprecated so you should probably move to a Service Account (OAuth 2.0) at some point.