Google Analytics Api 400错误请求

时间:2013-02-28 15:36:33

标签: c# google-api google-analytics-api http-status-code-400

我想从Google Analytics导入数据。

var gas = new AnalyticsService(auth);

var r = gas.Data.Ga.Get("ga:6332XXXX", "2013-02-01", "2013-02-11", "ga:visits");

r.Dimensions = "ga:date";
r.Sort = "ga:visits";
r.StartIndex = 1;

var data = r.Fetch();

我在Fetch方法中遇到400错误请求错误。我的代码出了什么问题?

我的完整代码如下:

var scope = AnalyticsService.Scopes.AnalyticsReadonly.ToString();
var clientId = "--------.apps.googleusercontent.com";
var keyFile = @"C:\-----------------privatekey.p12";
var keyPassword = "notasecret";

var desc = GoogleAuthenticationServer.Description;

var key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);

var client = new AssertionFlowClient(desc, key)
{
    ServiceAccountId = clientId,
    Scope = scope
};

var auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

var gas = new AnalyticsService(auth);

var r = gas.Data.Ga.Get("ga:6332XXXX", "2013-02-01", "2013-02-11", "ga:visits");

r.Dimensions = "ga:date";
r.Sort = "ga:visits";
r.StartIndex = 1;

var data = r.Fetch();

为了你的兴趣。

1 个答案:

答案 0 :(得分:1)

一些想法:

  1. 您的clientId应该是您的服务帐户电子邮件(不是ID)
  2. 您的范围应为“https://www.googleapis.com/auth/analytics.readonly
  3. 如果这不起作用,请检查@Martyn's answer