VLT-Website-Heading

How to use Postman with Google Cloud Platform APIs

Aug 20, 2021 11:33:32 PM / by Vu Long Tran

This is a brief guide on how you can use Postman with Google Cloud Platform APIs, by connecting your Postman to Google using OAuth 2.0. 

api-call-success

Create Access Token in Google Cloud Platform

Login to Google Cloud Console

In API & Services > Credentials > Create OAuth client ID

gcp-create-oauth-api-key

Add this under Authorized redirect URIs in your Google Console settings (https://console.cloud.google.com/apis/credentials/oauthclient).

gcp-create-oauth-api-key2

Set your Postman with one of the following callback URI/ redirect URI to: 

Please take note of the callback URI/s that you have set, as we want to later make sure it matches what we have set in Postman.

gcp-create-oauth-api-key-redirect

Set up Postman to use Google Cloud Platform APIs

Create a new "Authorization" in Postman.

Fill in your Authorization details and click "Get New Access Token" when you are ready. 

postman-gcp-configuration

When you click on "Get New Access Token" you will be asked to authorize Postman to interface with Google's APIs. 

So it will pop up a popup window and you will be asked to log into your Google account. Log in with your Google account used to access your Google Cloud Platform. 

This process will generate a OAuth ID and Google will check that the request came from an authorized redirect URL (which we have set).

You will receive an "Authentication complete" message when it is successful. Click Proceed afterwards.

postman-gcp-success

Select "Use Token" to start using your new Access Token.

postman-gcp-success2

This will add the Access Token under "Current Token> Access token> Available Tokens" to your Postman environment.

postman-access-token

You're all set. You can now run your Google Cloud Platform API calls.

Have fun using it now! 

 

Example Google Cloud API call

Here is an example, using a Google Cloud Platform API call:

Google Cloud API Method: instances.list

GET https://compute.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances

where 

  • project = your GCP project name (which you can get from your GCP URL)
  • zone = your GCP zone (which you can get from your GCP URL from your instance)

api-call-success

postman-test-apis-instance

 

Troubleshooting:

Authentication failed
Couldn't complete authentication. Check the Postman Console for more details.
authentication-failed-postman
 
Authorisation Error
Error 400: redirect_uri_mismatch
google-authorization-error2
 
Authorization Error
Error 400: redirect_uri_mismatch
You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy. \n\nIf you're the app developer, register the redirect URI in the Google Cloud Console.
 

Solution:

Login to Google Cloud Console

In API & Services -> Credentials create an OAuth 2.0 client ID

Add one of the following redirect URI/ callback URI under "Authorized redirect URIs" in your Google Console settings (https://console.cloud.google.com/apis/credentials/oauthclient). 

That is: redirect_uri:
You only need to set up one, so you can set any of URI you would like to set. 
 gcp-create-oauth-api-key-redirect
 
 

(Optional) Google Cloud Platform - Try this API

Google Cloud Platform offers this useful "Try this API" function on their API documentation pages.  This is helpful when you are unsure if it is the variable values you are using that is causing API calls to fail. So while it is not available for all APIs, when it is available you can populate it with the minimum required details and then test that the variable values that you want to test works first. Then you can put it into Postman and/or scripts or the application you are building that will leverage the APIs.
 
To use this function.
 
Go to Google Cloud Platform > Compute Engine> Documentation> Reference > GET Method: instances.list API documentation page.
 
It is available on the far right, with the words "Try it!"
 
gcp-try-this-api0
 
Then you populate it with basic details which you can then use to make a Google Cloud Platform API call. So once you are ready, click "Execute".
 
We will populate this with:
  • project = your GCP project name (which you can get from your GCP URL)
  • zone = your GCP zone (which you can get from your GCP URL from your instance). This will be for example, us-central1-a, us-central1-b, etc. 
gcp-try-this-api
 

It will be used to populate this Google Cloud API Method: instances.list behind the scenes in your web browser. 

Google Cloud API Method: instances.list

GET https://compute.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances

You should get a Status Code: 200 if it is successful.

postman-gcp-try-api
 

References:

Topics: google, cloud, apis

Vu Long Tran

Written by Vu Long Tran

Solutions Engineer APAC. ex-@Forrester consultant. Writing on #cloud #howto guides and #tech tinkering!