Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return inflight('auth', async () => {
// In Heroku environment, set GOOGLE_APPLICATION_CREDENTIALS as auth json object to be parsed
try {
const keys = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS)
authClient = nodeAuth.fromJSON(keys)
} catch (err) {
log.info('Couldn\'t get auth client credentials', err.message)
const {credential} = await google.auth.getApplicationDefault()
authClient = credential
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/datastore'
])
}
google.options({auth: authClient})
log.info('Google API auth successfully retrieved.')
return inflight('auth', async () => {
const credentialPath = process.env.GOOGLE_APPLICATION_CREDENTIALS
// In Heroku environment, set GOOGLE_APPLICATION_CREDENTIALS to 'parse_json' to avoid introducing new file
if (credentialPath === 'parse_json') {
log.info('Trying to parse client credentials via GOOGLE_APPLICATION_JSON.')
const keys = JSON.parse(process.env.GOOGLE_APPLICATION_JSON)
authClient = nodeAuth.fromJSON(keys)
} else {
const {credential} = await google.auth.getApplicationDefault()
authClient = credential
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/datastore'
])
}
google.options({auth: authClient})
log.info('Google API auth successfully retrieved.')
return authClient
async function main() {
const client = auth.fromJSON(keys);
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
}