【问题标题】:Google Analytics API 3 - Error:"invalid_grant", Description:"", Uri:""Google Analytics API 3 - 错误:“invalid_grant”,描述:“”,Uri:“”
【发布时间】:2014-08-19 14:51:23
【问题描述】:

我今天已经“用谷歌搜索”了这个问题,但零解决!

我正在尝试使用服务帐户构建一个非常简单的 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 = "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();

    }

}
}

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

注意,以下是我关注的一些来源:

Tutorial Followed to get above code

Useful but out of date

Google's code walk through - not in C# .Net

【问题讨论】:

  • 这与 Google Maps Javascript API v3 有什么关系?
  • 我什至没有意识到它被标记了!干杯我会删除它。
  • 除了下面的答案,请确保系统时钟设置为正确的时间。见stackoverflow.com/questions/11973162/…

标签: .net c#-4.0 google-analytics-api


【解决方案1】:

如果其他人有同样的问题,原来我的凭据有误:

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

【讨论】:

  • 我遇到了同样的问题。已经检查了服务帐户的电子邮件、.P12 证书和谷歌提供的标准密码“nodatasecret”。当我尝试使用IList<CalendarListEntry> calendarList = service.CalendarList.List().Execute().Items; 获取日历项目时,我仍然收到错误:错误:“invalid_grant”,描述:“”,Uri:“”。有什么想法吗?
  • 也许这个答案会有所帮助:Link
  • 是的,我遇到了同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 2014-12-04
相关资源
最近更新 更多