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.
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 |
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-tokenStep 2: Go inside the container so you can login to Google Cloud with your credentials.
docker exec -it gcloud-token /bin/bashStep 3: Run the login command then follow the steps displayed on the console.
gcloud auth login- Create a new collection e.g.
Token
- Add a new request to that collection.
- Now set the url as
localhost:7778/tokenand click the tabTeststo apply the script from below.
pm.globals.set("BEARER_TOKEN", responseBody);- Click
Sendto populate the globalBearer Tokenand start using it from any requests that specifiesBearer TokenasAuthorizationType and{{BEARER_TOKEN}}asToken.
- Create a new enviornment if you don't have one already.
- Set the environment variable and a default initial value and save.
-
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.
-
Instead of passing the globals in the
Testsscript, pass the snippet below:
pm.environment.set("BEARER_TOKEN", responseBody);- Click
Sendto populate theBearer Tokenand start using it for any requests in the environment that specifiesBearer TokenasAuthorizationType and{{BEARER_TOKEN}}asToken.
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());
})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.






