简体   繁体   中英

Access spreadsheet via Google API invalid_grant

I have created a google form, and keeping the form result in an excell sheet named yht_istekler. I am using following code snippet. taken from Create Spreadsheet using Google Spreadsheet API in Google drive in Java Though I am getting following error, what am I doing wrong?

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

Error :

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

Try adding access_type with the value offline to your auth request.

I had a very similar error to this not 2 days ago and this fixed the problem.

Also, just to confirm the clientID value should be similar to @developer.gserviceaccount.com They basically expect the email_address value from the console api credentials - not the client_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM