简体   繁体   English

Appscript中的授权请求header

[英]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 ? 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如果我只调用 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"}

The Auth token needs to be put into the header:需要将 Auth 令牌放入 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM