obtain a Google OAuth 2.0 Bearer token from a client and secret

3/31/2017

I have a client id and client secret for my Google Container Engine app obtained via Credentials and I just want to do some local testing of the JSON API endpoints.

How can I convert this into a Bearer token so that I can just get some work done? Is there a Google page where I provide these things and get a token that I can use in my app?

I don't want to have to write an entire OAuth handling mechanism at this point in time (which would use the flow described in oauthplayground). It's an app to be run only for my account, in headless mode, on a trusted machine, to manage my cluster.

NOTE: must not require any proprietary software installations (e.g. the Google SDK).

-- fommil
google-kubernetes-engine
google-oauth

2 Answers

3/31/2017

I have a client id and client secret for my Google Container Engine app obtained via Credentials and I just want to do some local testing of the JSON API endpoints.

Good start. I guess by "the JSON API endpoints" you mean the Google APIS. Make sure you created OAuth Client IDs and not one of the other options.

How can I convert this into a Bearer token so that I can just get some work done? Is there a Google page where I provide these things and get a token that I can use in my app?

Yes the OAuth Playground will do that for you. The detailed steps and sample code to consume the token is at How do I authorise an app (web or installed) without user intervention? (canonical ?)

I don't want to have to write an entire OAuth handling mechanism at this point in time (which would use the flow described in oauthplayground).

Follow the steps linked to above and you will see that you don't need to write any code at all. Once you have the refresh token (a one time procedure), you're all set. I exaggerate slightly, you do need one line of code to post the refresh token to the Google Oauth endpoint to fetch an access token. See the bottom of the linked answer for an example. Or you could just compose a curl to do it from the command line and put the Access Token into an environment variable.

I just wanted to avoid the whole thing and get a code printed on the screen

A bit like https://youtu.be/hfWe1gPCnzc?t=198

-- pinoyyid
Source: StackOverflow

3/31/2017

Google provides an API Client Library for Java, which itself depends on an OAuth client library.

For the project of 9Cards launcher for Android, within the back-end, we had to use this library to fetch applications usage statistics from Google Analytics. In our code, because it is a case of "server to server" authentication, we use a Service Account's credentials. The code issues a request from Google a short-lived OAuth2 Auth Token. The library may provide similar features if you use a Client-ID and Client-Secret.

Regarding the issue of licenses, the library is published under Apache License v2, so in that regard it is not too proprietary.

-- Diego E. Alonso-Blas
Source: StackOverflow