2

I have been using the Google.GData.Analytics and Google.GData.Client to retrieve some Google Analytics data though the Google Analytics API v2 in my web application.

The code was as follows:

string username = "[email protected]";
        string password = "myPasswordHere";
        string profileId = "ga:88376970";
        string _from = Convert.ToDateTime(dtpFrom.SelectedDate.Value.ToString()).ToString("yyyy-MM-dd");
       string _to = Convert.ToDateTime(dtpTo.SelectedDate.Value.ToString()).ToString("yyyy-MM-dd");
        AccountQuery AccountsQuery = new AccountQuery();
        service = new AnalyticsService("DoogalAnalytics");
        service.setUserCredentials(username, password);
        try
        {
            DataFeedUrl = "https://www.google.com/analytics/feeds/data";
            DataQuery PageViewQuery = new DataQuery(DataFeedUrl)
            {
                Ids = profileId ,
                Metrics = "ga:pageviews",
                Dimensions = "ga:date",
                Sort = "ga:date",
                GAStartDate = _from,
                GAEndDate = _to
            };
            StringBuilder strData = new StringBuilder();
            int maxValue = 0;
            int today = 0;
            List<int> gList = new List<int>();                
            foreach (DataEntry entry in service.Query(PageViewQuery).Entries)
            {
                var value = entry.Metrics.First().IntegerValue;
                gList.Add(value);
                maxValue = value > maxValue ? value : maxValue;
                today = entry.Metrics.Last().IntegerValue;
                strData.AppendFormat("{0},", value);
            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }

This code was working perfectly fine until about 5 days ago and I have been using this code for the last 7 or 8 months, but now suddenly I am facing the error

Execution of authentication request returned unexpected result: 404.

When I searched google. I have searched alot but could not find any solution. Any help or guidance will be greatly appreciated.

1 Answer 1

1

Client login which was discontinued / shutdown began on April 20 2015 and probably completed around May 26 2015. You can no longer use client login (Login and password) with the Google Analytics API, you need to switch to Oauth2. You will need to change your code to use Oauth2 or a service account.

Its best to grab the newest version of the client library which uses Google Analytics V3 you are using V2.

PM> Install-Package Google.Apis.Analytics.v3

If this is data you already own you may want to consider a service account. A service account will let you set up access to your google analytics account and it wont require a user to authenticate your code. If this is not your Google Analytics account but in fact one owned by another user then you will need to use Oauth2 and request authentication from the user.

My Tutorial: Google Analytics API Authentication with C#

Code ripped from tutorial above the tutorial is kept up to date this code may not be:

    string[] scopes = new string[] {
            AnalyticsService.Scope.Analytics,  // view and manage your Google Analytics data
            AnalyticsService.Scope.AnalyticsEdit,  // Edit and manage Google Analytics Account
            AnalyticsService.Scope.AnalyticsManageUsers,   // Edit and manage Google Analytics Users
            AnalyticsService.Scope.AnalyticsReadonly};     // View Google Analytics Data

    String CLIENT_ID = "6.apps.googleusercontent.com"; // found in Developer console
    String CLIENT_SECRET = "xxx";// found in Developer console
    // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
    UserCredential credential = 
            GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { 
                                                           ClientId = CLIENT_ID
                                                           , ClientSecret = CLIENT_SECRET 
                                                           }
                                                        , scopes
                                                        , Environment.UserName
                                                        , CancellationToken.None
                                                        , new FileDataStore("Daimto.GoogleAnalytics.Auth.Store")).Result;

AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
            {
             HttpClientInitializer = credential,
             ApplicationName = "Analytics API Sample",
            });

DataResource.GaResource.GetRequest request = service.Data.Ga.Get("ga:8903098", "2014-01-01", "2014-01-01", "ga:sessions");
request.MaxResults = 1000;
GaData result = request.Execute();
7
  • can you please provide some sample working code? I dont have enough experience with google apis and the code which i was using previously was downloaded from the internet...??
    – Namal159
    Commented Jun 2, 2015 at 11:50
  • the tutorial daimto.com/googleanalytics-authentication-csharp walks you though it at the bottom is the sample project Commented Jun 2, 2015 at 11:51
  • honestly speaking i don't have any idea how to get that.? can you please elaborate ????
    – Namal159
    Commented Jun 2, 2015 at 12:03
  • Click on the link it is a tutorial. Shows you how to do it. daimto.com/googleanalytics-authentication-csharp beyond giving you a tutorial and showing you the code how much more do elaboration do you need? Commented Jun 2, 2015 at 12:05
  • I have tried the code in this tutorial but it gives error like AnalyticsService.Scope.Analytics,V3.AnalyticsService does not contain a definition for scope
    – Namal159
    Commented Jun 2, 2015 at 12:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.