[Total: 0    Average: 0/5]

If you have ever wanted to test Google OAuth 2.0 flows from the command line you will like this short article.

This article is the second version. I wrote a previous article on using curl, but that version did not have a custom web server to handle the OAuth callback. This version includes a web server to automate the entire process. Read the first version for an introduction on using curl with OAuth.

Also, see my next article on how to refresh an access token:

Google OAuth 2.0 – Testing with Curl – Refresh Access Token

This article is for Windows Command Prompt users but should be easily adaptable to Linux and Mac also.

You will need your Google Client ID and Client Secret. These can be obtained from the Google Console under APIs & Services -> Credentials. In the following example code, these are stored in the file /config/client_secrets.json

These examples also use the program jq for processing the Json output. You can download a copy here.

In the following example, the Scope is cloud-platform. Modify to use the scopes that you want to test with. Here are a few scopes that you can test with:

OAuth 2.0 Scopes for Google APIs

Note that in this example the code uses three scopes. The second two are for the Client ID Token.

Details:
  1. Copy the following statements to a Windows batch file.
  2. Modify to fit your environment.
  3. Modify the script for the browser that you want to use.
  4. Run the batch file.
  5. A browser will be launched.
  6. The browser will go to https://accounts.google.com where you can complete the Google OAuth 2.0 authentication.
  7. The script will complete the OAuth 2.0 code exchange for a Token.
  8. The Token will be displayed in the command prompt.

The returned Token contains the Access Token, Refresh Token, and Client ID Token.

Windows Batch Script:

Web Server Python code (webserver.py):