com.google.api.client.auth.oauth2.TokenResponseException:400 Bad Request Invalid Grant Java Console Application

时间:2015-02-20 18:07:38

标签: java google-analytics console-application

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

"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);
    }
}}

知道怎么解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:0)

最后我找到了一个解决方案,认为我的主要问题是因为很多人都认为所有Google文档都很快就能读取,但我发现这篇文章:

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

仔细阅读答案,我只需添加评论,当您获得Grant Access(我的真正问题是这个)时,您必须将这些内容发送到您的帐户部分和属性部分,这是您的Google开发者帐户(xxxxxx-xr61clh @ developer .gserviceaccount.com),我希望这有帮助。