-1

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.

Venkat Singri
  • 104
  • 10

1 Answers1

0
  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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • thank you for your response : i spent almost 4 day, not getting the result i just copy pasted your code getting error as :Error:"unauthorized_client", Description:"", Uri:"", i gone through you tutorial also:( – Venkat Singri Mar 17 '16 at 12:06
  • 1
    did it pop up and request authentication? – Linda Lawton - DaImTo Mar 17 '16 at 12:10
  • No no popup nothing i got error at line GaData result = request.Execute(); – Venkat Singri Mar 17 '16 at 12:13
  • something is wrong up in your Authentication code then the authentication part needs to open a browser window and request access. Until the service is authenticated its not going to work. – Linda Lawton - DaImTo Mar 17 '16 at 12:14
  • ya sure the popup opened and got error like :400. That’s an error. Error: redirect_uri_mismatch Application: TestProduct The redirect URI in the request: http://localhost:52310/authorize/ did not match a registered redirect URI. – Venkat Singri Mar 17 '16 at 12:18
  • that is a different question and not related to your initial question. If you search you will find the answer. The redirect URI in the Google developers console must be the same as the website you are sending the request from in this case localhost/authorize – Linda Lawton - DaImTo Mar 17 '16 at 13:29