googleapis/google-api-java-client

Example for use with API key No OAUTH

LindaLawton opened this issue · 6 comments

All of your samples are for use with Oauth2 even your example for the google books api requires Oauth2.

oauth-2.0.md

Do you have an example for using a public api key. I don't need auth what i am trying to access is public data.

In general OAuth is recommended to authenticate your requests, and you can find more instructions on configuring credentials here.

Support for usage without OAuth is likely specific to the API - could you help me understand if you are looking to use the Books API in particular (or just pointing to it as an example), and whether you are running into any specific blockers for setting up authentication?

All Google apis support api key authorization, they may just not have any endpoints that would apply. YouTube, Drive, Calendar, Google analytics, books, are serval that I know for a fact do.

For example the google calendar api allows for it so that you can request public calendars like the google holiday calendars, YouTube search is also a public request that does not require authorization.

If you require authorization for things like that then you need a client id and client secrete and will have to request authorization of a user to access public data that you don't need access for. Not to mention then you will need to verify an application to access public data the verification team is just going to kick that out.

Are you saying this library does not support api key's?

Please see System parameters

image

Authenticate using API keys

I know for a fact that the following libraries do support it out of the box so i beg to differ that this is api specific.

  • Google api php client library
  • Google api python client library
  • Google api .net client library

Are you saying for a fact that this library does not support api key authentication?

@TimurSadykov Do you have more info on API Key support in Java auth?

@LindaLawton I see that the public documentation states that API Key support is language-specific, and there's only an example for Python.
https://cloud.google.com/docs/authentication/api-keys#using-with-client-libs

There is no 1st-class support yet. However, there is usually a way to pass an API key. Example for GAPIC clients.

@LindaLawton Could you try if something like this works for your API key usage pattern?

Compared to the OAuth sample for Books API:

Books books =
    new Books.Builder(
            GoogleNetHttpTransport.newTrustedTransport(),
            GsonFactory.getDefaultInstance(),
            new HttpCredentialsAdapter(credentials))
        .setApplicationName("BooksExample/1.0")
        .build();

Try passing in null for the HttpRequestInitializer argument, but explicitly set a GoogleClientRequestInitializer with the API key:

Books books =
    new Books.Builder(
            GoogleNetHttpTransport.newTrustedTransport(),
            GsonFactory.getDefaultInstance(),
            null)
        .setApplicationName("BooksExample/1.0")
        .setGoogleClientRequestInitializer(new BooksRequestInitializer(API_KEY))
        .build();