Moses
Moses

Reputation: 236

Authorization request header in Appscript

I need to call API data in Appscript I have Token: ZjA2YmYwNjYtNzgxYy00ZThjLWFjZjktMjEwYjM1O I have URL: https://webexapis.com/v1/devices?

How to authenticate token with the link. I get the below error if I only call URL

{"message":"The request requires a valid access token set in the Authorization request header.","errors":[{"description":"The request requires a valid access token set in the Authorization request header."}],"trackingId":"ROUTER_60ABA481-B37E-01BB-03DC-0AFDE82A03DC"}

Upvotes: 2

Views: 1054

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

The Auth token needs to be put into the header:

function makeRequest_(po) {
  var options,r;

  /*
    po - A JSON object with the following settings
    po.url - The url to make the request to
    po.token - The Auth Token
    po.method - POST or GET etc
  */
  
  if (!po.url) {
    console.error('No URL passed in. Parameters:' + JSON.stringify(po));
    return;
  }

  options = {};
  options.method = po.method;
  options.headers = {Authorization: 'Bearer ' + po.token};
  options.muteHttpExceptions = true;//Make sure this is always set

  r = UrlFetchApp.fetch(po.url, options);

  if (!r) {
    console.error('ERROR - getting response' );
    return;
  }
  
  //Logger.log('r.getResponseCode(): ' + r.getResponseCode());

  if (r && r.getResponseCode() !== 200) {//There should always be a response unless the
    //Fetch call failed

    var arg = po ? JSON.stringify(po) :"po not passed in";
    Logger.log('ERROR ' + r + '\npo:' + arg);

    return false;
  }
  
  //Logger.log('r: ' + r)
  return r;

}

Upvotes: 2

Related Questions