Skip to content

Instantly share code, notes, and snippets.

@thim81
Created January 16, 2022 13:53
Show Gist options
  • Save thim81/767d7627a45aa66978e9368e0bfc383e to your computer and use it in GitHub Desktop.
Save thim81/767d7627a45aa66978e9368e0bfc383e to your computer and use it in GitHub Desktop.
Auth0 - Outh2 token - Generate Postman Environment file
NODE_ENV=pipeline
BASEURL=https://example.com/api
OAUTH_BASE_URL=https://[enter-your-own].auth0.com
OAUTH_ACCESS_TOKEN_URL=https://[enter-your-own].auth0.com/oauth/token
OAUTH_CLIENT_ID=___INSERT_HERE_YOUR_AUTH0_CLIENT_ID___
OAUTH_CLIENT_SECRET=___INSERT_HERE_YOUR_AUTH0_CLIENT_SECRET___
OAUTH_AUDIENCE=___INSERT_HERE_YOUR_AUTH0_AUDIENCE___
OAUTH_ACCOUNT_ID=___INSERT_HERE_YOUR_AUTH0_ACCOUNT_ID___
OAUTH_GRANT_TYPE=client_credentials
OAUTH_TOKEN_URL=https://[enter-your-own].auth0.com
{
"scripts": {
"generate:k6:api:oauth:token": "node postman.env.js"
},
"dependencies": {
"axios": "^0.24.0",
"dotenv": "^9.0.2",
}
}
const fs = require('fs');
const path = require('path');
const request = require('axios');
const dotenv = require('dotenv');
dotenv.config({path: __dirname + '/.env'});
if (!process.env.OAUTH_ACCESS_TOKEN_URL) {
console.log('No ENV variables or .env file found.')
process.exit(1)
}
const currentTime = new Date().getTime();
const requestOptions = {
url: process.env.OAUTH_ACCESS_TOKEN_URL,
method: 'POST',
headers: {"Content-Type": "application/json"},
data: JSON.stringify({
"client_id": process.env.OAUTH_CLIENT_ID,
"client_secret": process.env.OAUTH_CLIENT_SECRET,
"audience": process.env.OAUTH_AUDIENCE,
'account_id': process.env.OAUTH_ACCOUNT_ID,
"grant_type": "client_credentials"
})
};
request(requestOptions)
.then(function (res) {
const resBody = res.data
let postmanEnv = {
"id": "7796f73b-b0d8-4b1a-98c8-9951304740b8",
"name": "postman environment",
"values": [
{
"key": "baseUrl",
"value": process.env.BASEURL,
"enabled": true
},
{
"key": "oauth_access_token",
"value": resBody.access_token,
"enabled": true
},
{
"key": "oauth_access_token_expires",
"value": resBody.expires_in,
"enabled": true
},
{
"key": "oauth_access_token_url",
"value": process.env.OAUTH_ACCESS_TOKEN_URL,
"enabled": true
},
{
"key": "oauth_grant_type",
"value": process.env.OAUTH_GRANT_TYPE,
"enabled": true
},
{
"key": "oauth_client_id",
"value": process.env.OAUTH_CLIENT_ID,
"enabled": true
},
{
"key": "oauth_client_secret",
"value": process.env.OAUTH_CLIENT_SECRET,
"enabled": true
},
{
"key": "oauth_audience",
"value": process.env.OAUTH_AUDIENCE,
"enabled": true
},
{
"key": "oauth_account_id",
"value": process.env.OAUTH_ACCOUNT_ID,
"enabled": true
},
{
"key": "oauth_base_url",
"value": process.env.OAUTH_BASE_URL,
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2021-01-07T15:15:27.355Z",
"_postman_exported_using": "Postman/7.36.1"
};
let postmanEnvOutput = JSON.stringify(postmanEnv, null, 2);
let currentFile = path.basename(__filename);
let postmanEnvFile = currentFile.substring(0, currentFile.length - 3);
fs.writeFileSync(__dirname + '/' + postmanEnvFile + '.json', postmanEnvOutput, 'utf8');
console.log('Auth0 access token updated');
})
.catch(function (err) {
// handle error
console.log(`POST ${process.env.OAUTH_ACCESS_TOKEN_URL} failure`, err.message)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment