我今天用谷歌搜索了这个问题的生命,分辨率为零!
我正在尝试使用服务帐户构建一个非常简单的 Google Analytics 数据请求控制台应用程序。我已在 Google Developers Console 中设置了所有必需的详细信息,但收到以下错误:
An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException'
occurred in Google.Apis.dll
Additional information: Error:"invalid_grant", Description:"", Uri:""
下面是我的控制台应用程序中的代码(隐藏了按键):
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Analytics;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using System;
namespace GoogleAnalyticsAPI
{
public class Program
{
public static void Main(string[] args)
{
string profileId = "12345678";
string serviceAccountEmail = "[email protected] /cdn-cgi/l/email-protection";
X509Certificate2 certificate = new X509Certificate2(@"PrivateKey.p12", "mypassword", X509KeyStorageFlags.Exportable);
// Create credentials
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Google Analytics API"
});
string startDate = "2014-07-01";
string endDate = "2010-07-31";
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(string.Format("ga:{0}", profileId), startDate, endDate, "ga:visits, ga:newVisits");
request.Dimensions = "ga:city";
GaData data = request.Execute();
}
}
}
任何帮助将不胜感激!
请注意,以下是我关注的一些来源:
按照教程获取上述代码 http://www.toplinestrategies.com/dotneters/net/accessing-and-querying-google-analytics-via-service-account-c/
有用但过时了 http://www.daimto.com/google-analytics-api-v3-with-c/罢工>
Google 的代码演练 - 不是在 C# .Net 中 https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide