2

I'm attempting to read/write to Google Sheet (owned by me) via Google Sheets API v4. To get Credential object I'm using the following code:

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {

    // Load client secrets.
    InputStream in = ResourceX.class.getResourceAsStream(CLIENT_SECRET_DIR);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    System.out.println(clientSecrets.toPrettyString());
    // When I print this ^^ it loads contents of secret key exactly as expected

    // Build flow and trigger user authorization request.
    Builder builder = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES); // error occurs on this line
            builder.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(CREDENTIALS_FOLDER)));
            GoogleAuthorizationCodeFlow flow = builder.setAccessType("offline").build();
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

I understand that IllegalArgumentException means that somehow the argument clientSecrets is illegal to be passed as parameter however when I print out the contents of json file (see print statement in code above) the contents seems to match the expected contents (I cannot print the contents on SO for security reasons but nothing was changed in the file since I downloaded it from Google).

I found a similar question here but there are no answers addressing the core question.

What possibly have I missed that may cause this exception?

Stacktrace:

Exception in thread "main" java.lang.IllegalArgumentException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
    at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
    at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:82)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder.<init>(GoogleAuthorizationCodeFlow.java:195)
    at global_projects.GoogleSheetsAPI.getCredentials(GoogleSheetsAPI.java:100)
    at global_projects.GoogleSheetsAPI.updateSheetByOauth2(GoogleSheetsAPI.java:116)
    at amz.Main.main(Main.java:78)

Update:

I think I found the problem. clientSecrets expects a json file from OAuth 2.0 client ID not a Service account key/jsonfile (as described in this link). However, since I am accessing my own spreadsheet is there a way to access it using a service account key/json file or must I use OAuth which would require me to provide consent to edit my own spreadsheet?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
S.O.S
  • 848
  • 10
  • 30
  • 1
    Could you include the stack trace? `IllegalArgumentException`s are usually accompanied by a description of what is wrong. – SeverityOne May 02 '18 at 04:31
  • You can still use service account, just add it's email address on the list of the accounts that can update, insert or delete data in the sheets. You can look for more info in this tutorial : [Google Spreadsheets and Python](https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html) (though it is in different coding language). – Mr.Rebot May 02 '18 at 15:43

1 Answers1

1

There are 2 ways of accessing your Google sheets file using the Google Client API.

  1. Create service account credentials for which you must give edit access in your particular sheet to the email which Google assigned for the account.

OR

  1. Create Oauth2 client id credentials to use with generating a credential object. This method will prompt you, by way of return URL, to provide authorisation in browser. Once you approve, you can query for a refresh token that enables you to refresh your access token without needing the browser flow approval again.

You are using method (1) and you're getting the IllegalArgumenException error because you have not given access to the email assigned to the service account.

John Fong
  • 11
  • 1