【问题标题】:com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request Invalid Grant java Console Applicationcom.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request Invalid Grant java Console Application
【发布时间】:2015-02-20 18:07:38
【问题描述】:

这是我的问题。我花了两天时间在谷歌上测试和搜索这个错误,但我不明白解决这个问题的方法。当我向谷歌提出请求时,谷歌说:

"error" : "invalid_grant"

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
}   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我导入这个罐子:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.GaData;
import com.google.api.client.http.HttpTransport;

这是我的代码

public static void analyticsExample()
{
// This is the .p12 file you got from the google api console by clicking generate new key
File analyticsKeyFile = new File("C:\\Analytics\\uvcanalytics-f44b89840853.p12");
// This is the service account email address that you can find in the api console
String apiEmail = "<794607131777-gl7vhg10l8vr8d7uln70udfl92r61clh@developer.gserviceaccount.com>";
GoogleCredential credential = null;
try {
    credential = new GoogleCredential.Builder()
    .setTransport(HTTP_TRANSPORT)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(apiEmail)
    .setServiceAccountScopes(Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY))
    .setServiceAccountPrivateKeyFromP12File(analyticsKeyFile).build();
} catch (Exception e) {
    System.out.print("\n Error A : " + e);
}


Analytics analyticsService = null;
try {
    analyticsService = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
    .setApplicationName("uvcanalytics")
    .build();
} catch (Exception e){System.out.print("\n ######### Bad Analytics service " + e );}


String startDate = "2015-02-15";
String endDate = "2015-02-15";
String mertrics = "ga:sessions";
String idProp = "ga:97649034";

// Use the analytics object build a query
Analytics.Data.Ga.Get get = null;
try {
    get = analyticsService.data().ga().get(idProp,startDate,endDate,mertrics);
    get.setDimensions("ga:date");
} catch (Exception e) {
    System.out.print("\n Error B : " + e);
}


// Run the query
GaData data = null;
try {
    data = get.execute();
} catch (Exception e) {
    System.out.print("\n Error C : " + e);
}

// Do something with the data
if (data.getRows() != null) {
    for (List<String> row : data.getRows()) {
        System.out.println(row);
    }
}}

知道如何解决这个问题吗?谢谢。

【问题讨论】:

  • 搜索它有没有给你任何答案?你说你不明白解决方案。这是否意味着您找到了解决方案但不知道如何应用?

标签: java google-analytics console-application


【解决方案1】:

最后我找到了一个解决方案,我认为我的主要问题是很多人都认为所有谷歌文档都很大,无法快速阅读,但我找到了这篇文章:

Google Analytics authorization in java 从 [我遇到同样的问题,我花了大约 1 小时在 v3 文档中找到它] 开始]

仔细阅读答案,我只是添加评论,当您授予访问权限(我真正的问题是这个)时,您必须在您的帐户部分和您的 Google 开发人员帐户的属性部分执行此操作(xxxxxx-xr61clh@developer .gserviceaccount.com),希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 2016-05-20
    • 2017-04-14
    • 2012-09-07
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多