2

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
{
  "
Community
  • 1
  • 1
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141

1 Answers1

4

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

scgough
  • 5,099
  • 3
  • 30
  • 48
  • How can I set access_type? Could you give more specific example_ – Ahmet Karakaya Jun 03 '15 at 20:01
  • I wasn't coding in Java but will see if I can work it out. Also, can I confirm setServiceAccountId has the client ID value from your api credentials in Google developer console? – scgough Jun 03 '15 at 20:06
  • yeap I have a clientID and do not want to write it here – Ahmet Karakaya Jun 03 '15 at 20:08
  • And to confirm its along the lines of @developer.gserviceaccount.com ? I'm wondering if that is actually the issue. Wrong value in client Id (I had that too!) – scgough Jun 03 '15 at 20:08
  • 2
    Just out of interest, try the 'email address' setting in its place in your code. The one ending @developer.gserviceaccount... – scgough Jun 03 '15 at 20:17
  • Ah great. Edited my answer above. I read that even though they label up 'client_id' in the dev console, the actual value they expect is 'email_address' ?!? – scgough Jun 03 '15 at 20:31
  • hi if you could mark the answer @mmc18 that'd be great – scgough Jun 05 '15 at 08:20
  • sorry I have forgotten to upvote it because of exiting of find the solution :) – Ahmet Karakaya Jun 05 '15 at 11:02