Google AnalyticsAPI 3 - 错误:"invalid_grant",描述:"",Uri:""

Mat*_*ebb 3 .net google-analytics-api c#-4.0

今天我用谷歌搜索"解决"这个问题的生命!

我正在尝试使用服务帐户构建一个非常简单的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:""
Run Code Online (Sandbox Code Playgroud)

下面是我的控制台应用程序中的代码(隐藏了键):

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

    }

}
}
Run Code Online (Sandbox Code Playgroud)

这里的任何帮助将不胜感激!

请注意,以下是我遵循的一些来源:

教程接着获得上面的代码

有用但过时了

Google的代码遍历 - 而不是C#.Net

Mat*_*ebb 10

事实证明我有错误的凭据,如果其他人有同样的问题:

  1. 仔细检查您是否使用了以"@ developer.gserviceaccount.com"结尾的服务帐户电子邮件
  2. 检查下载的.p12文件是否位于调试文件夹中,并提供正确的私钥密码(在您创建服务帐户时提供给您).