View the original community article here
Last tested: Oct 21, 2020
Are you lazy like me and want to avoid having to refresh your auth token periodically when using the Looker API in Postman? This guide will help you to configure your Postman collection with a pre-request script that checks if the current token is expired and authenticates you and stores the access token if it is.
STEP 1: Read the article
The script below is based off the concepts in this article, which is worth a quick read:
https://medium.com/@allen.helton/how-to-automate-oauth2-token-renewal-in-postman-864420d381a0
STEP 2: Create AND INIT Variables
We'll want to create the same variables laid out in the article, except instead of the Basic_Auth
collection variable, we'll create two collection variables called id
and secret
. We'll set the Initial Value and Current Value of the three Collection Variables as such:
id: your API Client ID from Looker
secret: your API Client Secret from Looker
Auth_Url: the /login endpoint of your Looker instance (i.e. https://instance.looker.com:19999/api/3.1/login)
Step 3: Add pre-request script
Edit your Looker collection and add the following as a pre-request script. This will run before every request in that collection (the auth call will only run if the token is expired).
var url = pm.variables.get("Auth_Url");
const postRequest = {
url: url,
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "client_id", value: pm.variables.get("id"), disabled: false},
{key: "client_secret", value: pm.variables.get("secret"), disabled: false}
]
}
};
var tokenDate = new Date(2010,1,1);
var tokenTimestamp = pm.environment.get("OAuth_Timestamp");
if(tokenTimestamp){
tokenDate = Date.parse(tokenTimestamp);
}
var expiresInTime = pm.environment.get("ExpiresInTime");
if(!expiresInTime){
expiresInTime = 3600000; // Set default expiration time to 5 minutes
}
if((new Date() - tokenDate) >= expiresInTime)
{
pm.sendRequest(postRequest, (error, response) => {
console.log(error ? error : response.json());
var authresponse = response.json();
pm.environment.set("OAuth_Token", authresponse['access_token']);
pm.environment.set("OAuth_Timestamp", new Date());
// Set the ExpiresInTime variable to the time given in the response if it exists
if(response.json().expires_in){
expiresInTime = response.json().expires_in * 1000;
}
pm.environment.set("ExpiresInTime", expiresInTime);
});
};
This content is subject to limited support.