我一派的生活出了这个问题今天零的决心!
I've 'googled' the life out of this issue today with zero resolve!
我想建立使用服务帐户很简单的谷歌Analytics(分析)数据请求控制台应用程序。我已经建立了在谷歌开发者控制台所有需要的细节,但我收到以下错误:
I am trying to build a very simple Google Analytics data request console app using a Service account. I have set up all the required details in the Google Developers Console but I am getting the following error:
An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException'
occurred in Google.Apis.dll
Additional information: Error:"invalid_grant", Description:"", Uri:""
下面是我的控制台应用程序的code(连键隐藏):
Below is the code in my console app (with keys hidden):
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 = "123456789abcdefghijklmnopq@developer.gserviceaccount.com";
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();
}
}
}
在这里任何帮助将是很大的AP preciated!
Any help here would be greatly appreciated!
请注意,这里有一些资源,我跟着:
Note, here are some sources I've followed:
教程随后获得上述code
有用的,但过时 < /击>
谷歌的code穿行 - 不是在C#.NET
事实证明,我有错误的凭据,如果任何人有同样的问题:
It turns out I had the wrong credentials, if anyone else has the same problem:
双击您正在使用的服务帐户结尾的电子邮件勾选@ developer.gserviceaccount.com 检查下载的的.p12
文件位于你的debug文件夹,你提供了正确的私钥密码(提供给您,当您创建您的服务帐户)。
Double check you're using the service account email ending "@developer.gserviceaccount.com"
Check the downloaded .p12
file is located in your debug folder and you supply the correct private key password (given to you when you create your service account).