Execution of authentication request returned unexp

2019-07-22 08:19发布

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 = "abc@gmail.com";
        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条回答
时光不老,我们不散
2楼-- · 2019-07-22 09:21

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();
查看更多
登录 后发表回答