简体   繁体   中英

The remote server returned an error: (404) Not Found. google implentation in c#

I'm using OAuth2 credentials, getting 404 error:

using Google.Analytics;
using Google.GData.Analytics;

    void Analytics()
        {
            try
            {
                string userName = ConfigurationManager.AppSettings["username"];
                string passWord = ConfigurationManager.AppSettings["password"];
                string gkey = "key=api _key";
                string dataFeedUrl = "https://www.googleapis.com/analytics/v3/data/ga";//+ gkey;
                AccountQuery query = new AccountQuery();
                AnalyticsService service = new AnalyticsService("Web App");
                service.setUserCredentials(userName, passWord);

                DataQuery query1 = new DataQuery(dataFeedUrl);
                query1.Ids = "ga:123456789";
                query1.Metrics = "ga:visits,ga:sessions,ga:pageValue,ga:bounces,ga:bounceRate,ga:pageviews";
                query1.Dimensions = "ga:city,ga:date";
                query1.GAStartDate = ("2016-03-15");//DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd");
                query1.GAEndDate = ("2016-03-17");//DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
                query1.ExtraParameters = gkey;
                DataFeed dataFeedVisits = service.Query(query1);
                foreach (DataEntry entry in dataFeedVisits.Entries)
                {
                    string st = entry.Title.Text;
                    string ss = entry.Metrics[0].Value;
                    int _intVisists = Int32.Parse(ss);
                    Response.Write("<br/>");
                    Response.Write("Total Visits : " + ss);
                    Response.Write("<br/>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error : " + ex);
            }
        }

Exception is :

Execution of request failed: https://www.googleapis.com/analytics/v3/data/ga?key=api_key&dimensions=ga:city,ga:date&end-date=2016-03-17&ids=ga:123456789&metrics=ga:visits,ga:sessions,ga:pageValue,ga:bounces,ga:bounceRate,ga:pageviews&start-date=2016-03-15

Somehow its redirecting to https://www.google.com/accounts/ClientLogin which is shutdown by google.

  1. client login was shut down in May 2015, there for you need to use Open authentication.
  2. you are using the GData library which requires data be returnd as XML.
  3. you are requesting against the Google Analytics V3 API, which is not a gdata api and returns data as JSon.

Solution:

Install the current version of the Google .net client library

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

Authentication:

string[] scopes = new string[] {AnalyticsService.Scope.AnalyticsReadonly};      // View Google Analytics Data

var clientId = "[Client ID]";      // From https://console.developers.google.com
var clientSecret = "xxx";          // From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId,
                                                                                 ClientSecret = clientSecret},
                                                            scopes,
                                                            Environment.UserName,
                                                            CancellationToken.None,
                                                            new FileDataStore("Daimto.GoogleAnalytics.Auth.Store")).Result;

create an analytics service

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

request data

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();

code ripped from my Google Analytis api tutorail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM