Norbert
Norbert

Reputation: 1

Access token request fails when using Google App Script to connect to Cisco API

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&#58;&#47;&#47;id&#46;cisco&#46;com&#47;oauth2&#47;default&#47;v1&#47;token" on this server.<P>
Reference&#32;&#35;18&#46;42c2d17&#46;1717456967&#46;c4d3dd
<P>https&#58;&#47;&#47;errors&#46;edgesuite&#46;net&#47;18&#46;42c2d17&#46;1717456967&#46;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

Answers (0)

Related Questions