使用服务帐户访问Google AnalyticsAPI会返回invalid_grant错误

时间:2016-01-29 05:48:39

标签: java google-oauth google-analytics-api google-api-java-client service-accounts

我们有一个Google Analytics帐户,可以跟踪最常搜索的字词。我想在java中获取该报告。为此,我尝试按照以下链接:Hello Analytics API: Java quickstart for service accounts

  1. 创建项目并启用了Analytics API
  2. 创建了一个客户端ID。创建了一个服务帐户。当提示输入密钥类型时,在创建服务帐户时选择P12,并将生成的密钥保存为client_secrets.p12
  3. 向Google Analytics帐户添加了服务帐户电子邮件ID(208051463715-compute208051463715-compute@developer.gserviceaccount.com)至@developer.gserviceaccount.com。阅读&分析权限已分配给它
  4. 安装了Google客户端库并设置了示例代码 我们使用开发者控制台中的相应值替换了示例代码中存在的SERVICE_ACCOUNT_EMAIL和KEY_FILE_LOCATION的值。
  5. SERVICE_ACCOUNT_EMAIL:208051463715-compute208051463715-compute@developer.gserviceaccount.com)to@developer.gserviceaccount.com

    KEY_FILE_LOCATION:我们将文件保存在我们保存analytics.java file.below link的同一目录中,并按照下面提到的步骤进行操作。

    但是我们遇到了以下错误:

    运行java类时,我们会遇到异常,如下所示:

      

    com.google.api.client.googleapis.auth.oauth2.GoogleCredential@39fb3ab6       com.google.api.client.auth.oauth2.TokenResponseException:400错误请求       {         "错误":" invalid_grant"       }         在com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)         在com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)         在com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)         在com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)         在com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)         在com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)         在com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)         在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)         在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)         在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)         在com.hm.backstage.components.util.HelloAnalytics.getFirstProfileId(HelloAnalytics.java:68)         在com.hm.backstage.components.util.HelloAnalytics.main(HelloAnalytics.java:33 **)

0 个答案:

没有答案