Skip to content

naz-mul/postman-gcloud-token

Repository files navigation

Postman GCloud Token Setter


As a back-end developer I spend a lot of my time working with APIs from many different providers, either as for work or for hobby projects. To understand how these APIs work I tend to make use of Postman.

My recent development time has involved working with Google APIs. Google APIs require authentication which essentially boils down to you providing a bearer token. These bearer tokens are short lived and tend to require frequent regeneration. As you can imagine, this gets tedious fairly quickly when sending API requests to your endpoint.

To make my life slightly less cumbersome, I have decided to create this project.

Pre-requisites

These applications and command line tools need to be installed.

Tool Version
NodeJS >= 14 < 15 (required for development only)
Google Cloud CLI latest (required for development only)
Docker latest
Postman latest

Set up the server

Step 1: Run this command to create the container called gcloud-token which will run on port 7778 and will be automatically restarted. (Specifying the host explicitly will prevent anyone on your network from pulling your tokens.)

docker run --name=gcloud-token --restart=always -p 127.0.0.1:7778:80 -d nalam/postman-gcloud-token

Step 2: Go inside the container so you can login to Google Cloud with your credentials.

docker exec -it gcloud-token /bin/bash

Step 3: Run the login command then follow the steps displayed on the console.

gcloud auth login

Configuring Postman

Configure as a global environment variable

  1. Create a new collection e.g. Token

Create a new collection

  1. Add a new request to that collection.

Add a new request

  1. Now set the url as localhost:7778/token and click the tab Tests to apply the script from below.

Set URL and Test Script

pm.globals.set("BEARER_TOKEN", responseBody);
  1. Click Send to populate the global Bearer Token and start using it from any requests that specifies Bearer Token as Authorization Type and {{BEARER_TOKEN}} as Token.

Setting bearer token

Configure as an environment variable

  1. Create a new enviornment if you don't have one already.

Create new environment

  1. Set the environment variable and a default initial value and save.

Set environment variable

  1. Select the environment you have just created from the top-right corner, and follow steps 1, 2 and 3 from the steps as outlined here.

  2. Instead of passing the globals in the Tests script, pass the snippet below:

pm.environment.set("BEARER_TOKEN", responseBody);
  1. Click Send to populate the Bearer Token and start using it for any requests in the environment that specifies Bearer Token as Authorization Type and {{BEARER_TOKEN}} as Token.

Configure as per request variable

Create a new API Request in Postman, then select Pre-request Script. Copy and paste this snippet in the textbox.

pm.sendRequest('http://127.0.0.1:7778/token', (err, response) => {
    console.log(response.text());
    pm.variables.set("BEARER_TOKEN", response.text());
})

Create pre-request script

Finally, prepare the header to use bearer token by selecting the Authorization tab from the API Request tab in Postman, choose Bearer Token for Type and {{BEARER_TOKEN}} for Token.

Setting bearer token