【发布时间】:2013-12-24 19:36:35
【问题描述】:
我正在尝试使用 API v3 访问 Google Analytics(分析)信息
// This is the physical path to the key file you downloaded when you created your Service Account
String key_file = @"C:\Users\path\XXXXX-privatekey.p12";
// Is the "Email Address", not the "Client ID" one!!!
String client_id = "0000000-xxxxx@developer.gserviceaccount.com";
// Probably the password for all is "notasecret"
String key_pass = "notasecret";
private void button1_Click(object sender, System.EventArgs e)
{
try
{
String scope_url = "https://www.googleapis.com/auth/" + Google.Apis.Analytics.v3.AnalyticsService.Scopes.Analytics.ToString().ToLower();
//scope_url = "https://www.googleapis.com/auth/analytics";
//scope_url = "https://www.googleapis.com/auth/analytics.readonly";
AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;
X509Certificate2 key = new X509Certificate2(key_file, key_pass, X509KeyStorageFlags.Exportable);
AssertionFlowClient client = new AssertionFlowClient(desc, key) { ServiceAccountId = client_id, Scope = scope_url };
OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);
AnalyticsService gas = new AnalyticsService(auth);
// Creating our query
DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:1983192", "2013-11-01", "2013-11-30", "ga:visits,ga:pageviews");
//r.Dimensions = "ga:date";
//d: Execute and fetch the results of our query
GaData d = r.Fetch();
// At this point, d should contain the number of visitors you got between dates
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但我总是得到下一个错误
Google.Apis.Requests.RequestError 用户没有任何 Google Analytics 帐户。 [403] 错误 [ 消息[用户没有任何 Google Analytics 帐户。] 位置[-] 原因[insufficientPermissions] 域[全局] ]
我在https://cloud.google.com/console 中创建应用程序 如您所见,创建凭据 拥有有效的 Analytics 帐户并在控制台的 API 部分获得授权。
会发生什么?
【问题讨论】:
标签: c#-4.0 google-analytics-api