10

Is there a way to use "Google Sheet Java API" with API key not with OAuth which is given in their examples

https://developers.google.com/sheets/api/quickstart/java

I know you can use HTTP request to get data with API key but I was thinking if there is a way to do this with the google provided Java API so that I don't have to parse JSON for every request.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Mayank Wadhwa
  • 1,673
  • 17
  • 20
  • 3
    You can only use the API key for consuming data - if you want to *write*, you will need to use `OAuth`. – tehhowch Dec 24 '18 at 00:38

4 Answers4

8

I didn't find any official way to achieve this, but I was able to do it as described in Acquiring and using an API key:

After you have an API key, your application can append the query parameter key=yourAPIKey to all request URLs.

by using a request interceptor and adding the key query parameter manually, like this:

private Sheets getSheets() {
    NetHttpTransport transport = new NetHttpTransport.Builder().build();
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer httpRequestInitializer = request -> {
        request.setInterceptor(intercepted -> intercepted.getUrl().set("key", API_KEY));
    };

    return new Sheets.Builder(transport, jsonFactory, httpRequestInitializer)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

public List<List<Object>> getValues(String spreadsheetId, String range) throws IOException {
    return getSheets()
            .spreadsheets()
            .values()
            .get(spreadsheetId, range)
            .execute()
            .getValues();
}
arekolek
  • 9,128
  • 3
  • 58
  • 79
0

Yes you can, essentially you need something like the following:

public NetHttpTransport netHttpTransport() throws GeneralSecurityException, IOException {
    return GoogleNetHttpTransport.newTrustedTransport();
}

public JacksonFactory jacksonFactory() {
    return JacksonFactory.getDefaultInstance();
}

private Set<String> googleOAuth2Scopes() {
    Set<String> googleOAuth2Scopes = new HashSet<>();
    googleOAuth2Scopes.add(SheetsScopes.SPREADSHEETS_READONLY);
    return Collections.unmodifiableSet(googleOAuth2Scopes);
}

public GoogleCredential googleCredential() throws IOException {
    File serviceAccount = new ClassPathResource("serviceAccount.json").getFile();
    return GoogleCredential.fromStream(new FileInputStream(serviceAccount))
            .createScoped(googleOAuth2Scopes());
}

public Sheets googleSheets() {
    return new Sheets(netHttpTransport(), jacksonFactory(), googleCredential());
}

You can read more about the serviceAccount.json here: https://cloud.google.com/iam/docs/creating-managing-service-account-keys


The above was taken from an example Spring Boot project I did integrating with Google's APIs: https://github.com/ciscoo/spring-boot-google-apis-example

Cisco
  • 20,972
  • 5
  • 38
  • 60
  • Isn't this still OAuth rather than API Key authentication? Just using a service account rather than user account. – arekolek Aug 03 '20 at 11:41
0

This is possible. Here's how I initialize an instance of Sheets using an API key instead of OAuth:

SheetsRequestInitializer REQUEST_INITIALIZER = new SheetsRequestInitializer(API_KEY);
JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Sheets service = new Sheets.Builder(httpTransport, JSON_FACTORY, null)
            .setApplicationName("HelloWorld")
            .setGoogleClientRequestInitializer(REQUEST_INITIALIZER)
            .build();
thenewpotato
  • 101
  • 1
  • 9
-1

you can also use this code:

Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .setGoogleClientRequestInitializer(CommonGoogleClientRequestInitializer.newBuilder().setKey(API_KEY).build())
                .build();