远程服务器返回错误:(404)Not Found。谷歌在c#中的实施

时间:2016-03-17 10:26:26

标签: c# google-analytics-api gdata

我正在使用OAuth2凭据,收到404错误:

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

例外是:

  

执行请求失败: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

以某种方式将其重定向到https://www.google.com/accounts/ClientLogin,谷歌已将其关闭。

1 个答案:

答案 0 :(得分:0)

  1. 客户端登录已于2015年5月关闭,您需要使用开放式身份验证。
  2. 您正在使用GData库,该库需要将数据作为XML返回。
  3. 您要求使用的是Google Analytics V3 API,它不是gdata api,而是以JSon的形式返回数据。
  4. <强>解决方案:

    安装当前版本的Google .net客户端库

    PM&GT;安装包Google.Apis.Analytics.v3

    <强>认证

    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;
    

    创建分析服务

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

    代码从我的Google Analytis api tutorail

    中删除