Skip to content

IllegalArgumentException being thrown when trying to authenticate with google sheets api #93

Open

Description

@aeslami3574

(Please fill out these details before submitting an issue)

Sample Name

Modified version of https://developers.google.com/sheets/api/quickstart/java

private Credential getCredentials(NetHttpTransport httpTransport) {
    try {
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(new FileInputStream(credentialsFilePath)));
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new File(credentialsFilePath))).setAccessType("offline")
                .build();
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setPort(8888).build()).authorize("user");
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Expected Behavior

Was expecting to receive a Credential object after successfully authenticating with the google api

Actual

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)

Specifications

  • Java version: 11.0.5
  • OS: Linux (Debian 10.2 Buster)

Activity

  1. sqrrrl commented on Dec 3, 2019

    @sqrrrl
    Member

    Check your client secrets file to make sure it's correct. Possible that you downloaded credentials in the wrong form.

    That error indicates it's not in the expected format as isn't either for a server-side web app or installed (desktop) app. Corresponding code: https://github.com/googleapis/google-api-java-client/blob/master/google-api-client/src/main/java/com/google/api/client/googleapis/auth/oauth2/GoogleClientSecrets.java

  2. self-assigned this
    on Dec 3, 2019
  3. sqrrrl commented on Jan 21, 2020

    @sqrrrl
    Member

    Ping @aeslami3574 -- please verify your credentials are the correct type. Otherwise will close issue as not reproducible.

  4. cdolek commented on Oct 15, 2020

    @cdolek

    I had the same issue using the wrong credentials.json I have downloaded for a service account, created to access google sheets from a web application. I was banging my head until I discovered that I needed an OAuth credentials, set "Web Application" as application type json file that has been downloaded from google cloud console.

    My credentials file should looks like this:

    {
    	"web": {
    		"client_id": String,
    		"project_id": String,
    		"auth_uri": String,
    		"token_uri": String,
    		"auth_provider_x509_cert_url": String,
    		"client_secret": String
    	}
    }
    
  5. Johnny850807 commented on Apr 9, 2022

    @Johnny850807

    My workaround: Don't use the client secrets, instead, directly build the credentials.

    Import this library

    <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>1.3.0</version>
    </dependency>
    InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }
    GoogleCredentials googleCredentials = GoogleCredentials.fromStream(in).createScoped(SCOPES);
    
    Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpCredentialsAdapter(googleCredentials))
                    .setApplicationName(APPLICATION_NAME)
                    .build();
  6. weehong-1 commented on Jul 11, 2022

    @weehong-1

    My workaround: Don't use the client secrets, instead, directly build the credentials.

    Import this library

    <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>1.3.0</version>
    </dependency>
    
    InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }
    GoogleCredentials googleCredentials = GoogleCredentials.fromStream(in).createScoped(SCOPES);
    
    Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpCredentialsAdapter(googleCredentials))
                    .setApplicationName(APPLICATION_NAME)
                    .build();

    This is the most accurate answer until today.
    Hopefully, more developers can see this.

  7. velikanov commented on Sep 15, 2022

    @velikanov

    My workaround: Don't use the client secrets, instead, directly build the credentials.

    Import this library

    <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>1.3.0</version>
    </dependency>
    
    InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }
    GoogleCredentials googleCredentials = GoogleCredentials.fromStream(in).createScoped(SCOPES);
    
    Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpCredentialsAdapter(googleCredentials))
                    .setApplicationName(APPLICATION_NAME)
                    .build();

    Works like a charm!
    If you see this - use this

    THANK YOU @Johnny850807
    Hey @google come on, update your docs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions