通过Google API访问电子表格invalid_grant

时间:2015-06-03 19:57:16

标签: java google-api google-sheets gmail-api

我创建了一个google表单,并将表单结果保存在名为yht_istekler.的excell表中 我正在使用以下代码段。取自Create Spreadsheet using Google Spreadsheet API in Google drive in Java 虽然我得到了以下错误,但我做错了什么?

  List<TrenSeferScheduleEvent> trenSeferScheduleEventList = new ArrayList<TrenSeferScheduleEvent>();

  URL SPREADSHEET_FEED_URL;
  SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

  File p12 = new File("./conf/key.p12");

  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  List<String> SCOPES_ARRAY = Arrays.asList("https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds");
  GoogleCredential credential = new GoogleCredential.Builder()
          .setTransport(httpTransport)
          .setJsonFactory(jsonFactory)
          .setServiceAccountId("clientID")
          .setServiceAccountScopes(SCOPES_ARRAY)
          .setServiceAccountPrivateKeyFromP12File(p12)
          .build();

  SpreadsheetService service = new SpreadsheetService("yht_istekleri");
  service.setOAuth2Credentials(credential);

错误:

com.google.gdata.util.AuthenticationException: Failed to refresh access token: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:260)
    at com.google.gdata.client.GoogleAuthTokenFactory.handleSessionExpiredException(GoogleAuthTokenFactory.java:702)
    at com.google.gdata.client.GoogleService.handleSessionExpiredException(GoogleService.java:738)
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:649)
    at com.google.gdata.client.Service.getFeed(Service.java:1017)
    at com.ahmetk.ticketsystem.yht.dao.TrenSeferScheduleEventDaoImpl.getTrenSeferScheduleEventList(TrenSeferScheduleEventDaoImpl.java:66)
    at com.ahmetk.ticketsystem.yht.dao.TrenSeferScheduleEventDaoImpl.main(TrenSeferScheduleEventDaoImpl.java:149)
Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "

1 个答案:

答案 0 :(得分:4)

尝试将值access_type的{​​{1}}添加到您的身份验证请求中。

我在2天前遇到了类似的错误,这解决了这个问题。

另外,要确认offline值应与clientID类似 他们基本上期望来自控制台api凭据的@developer.gserviceaccount.com值 - 而不是email_address