Reputation: 1
I try to use Google App Script (GAS) to access a Cisco API. To access Cisco's API I need to request an access token first. This token request fails when using GAS. It works fine using Curl on a Linux device or via VS Code.
Hope anyone did this already and can help?
My code on GAS:
async function getAccessToken() {
const clientID = 'UUUUUUUU';
const clientSecret = 'PPPPPPPP';
const url = "https://id.cisco.com/oauth2/default/v1/token"
const body = `grant_type=client_credentials&client_id=${clientID}&client_secret=${clientSecret}`;
const headers = {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
};
const options = {
muteHttpExceptions: true,
payload: body,
headers,
method: 'POST',
};
try{
var response = await UrlFetchApp.fetch(url,options);
} catch(err) {
console.log(err)
}
const test = await response.getContentText();
console.log(test);
}
My bad response in GAS:
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>
You don't have permission to access "http://id.cisco.com/oauth2/default/v1/token" on this server.<P>
Reference #18.42c2d17.1717456967.c4d3dd
<P>https://errors.edgesuite.net/18.42c2d17.1717456967.c4d3dd</P>
</BODY>
</HTML>
My code in VS Code:
async function getAccessToken() {
const clientID = 'UUUUUUUU';
const clientSecret = 'PPPPPPPP';
const url = "https://id.cisco.com/oauth2/default/v1/token"
const body = `grant_type=client_credentials&client_id=${clientID}&client_secret=${clientSecret}`;
const headers = {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
};
const options = {
muteHttpExceptions: true,
body,
headers,
method: 'POST',
};
try{
var response = await fetch(url,options);
} catch(err) {
console.log(err)
}
const test = await response.json();
console.log(test);
}
My good response in VS Code:
{
token_type: 'Bearer',
expires_in: 3600,
access_token: '<- REMOVED ->',
scope: 'customscope'
}
Upvotes: 0
Views: 47