通过 Google API invalid_grant 访问电子表格

Access spreadsheet via Google API invalid_grant

我创建了一个 google 表单,并将该表单结果保存在名为 yht_istekler. 的 excel sheet 中 我正在使用以下代码片段。摘自 虽然我收到以下错误,但我做错了什么?

  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
{
  "

尝试将值 offlineaccess_type 添加到您的授权请求中。

我在 2 天前遇到了一个与此非常相似的错误,这解决了问题。

此外,只是为了确认 clientID 值应该类似于 @developer.gserviceaccount.com 他们基本上期望来自控制台 api 凭据的 email_address 值 - 而不是 client_id