Apigee-Auth retrieves Apigee OAuth2 Access Tokens from a username/password. It automatically utilize refresh tokens when the current access token has expired so that we can always retrieve the next valid access token.
$ npm install apigee-auth
const ApigeeAuth = require('apigee-auth')
const apigeeAuth = new ApigeeAuth('username', 'password')
apigeeAuth.getToken().then(response => {
console.log(response.access_token)
})
If you have two factor authentication enabled, you could pass in your TOTP secret as the 3rd parameter.
const apigeeAuth = new ApigeeAuth('username', 'password', 'your-2fa-totp-secret')
Please note that if you are using 2FA this way, you are no longer using two factor authentication because you may have compromised your 2FA secret by storing your 2FA secret outside your mobile device or mobile key.
This optional parameter is provided as convenience if you must require 2FA in your logins but still want to automate Apigee Management APIs in your environment.
getToken()
- returns: Promise with the JSON output as specified by the Apigee OAuth2 Access Tokens documentation for POSTing to https://login.apigee.com/oauth/token
If you use axios, you could create a reusable instance using axios interceptors:
const client = axios.create()
client.interceptors.request.use(
async config => {
const { access_token } = await apigeeAuth.getToken()
return Promise.resolve({
...config,
headers: {
Authorization: `Bearer ${access_token}`
}
})
},
err => Promise.reject(err)
)