简体   繁体   中英

Google Apps Script get response from external API

I'm using a google apps script to get data from an external API.

This is the code I'm using:

function cenas(){
 var url='https://app.ecwid.com/api/v1/XXX/orders';
 var response = UrlFetchApp.fetch(url);
  Logger.log(response);
}

I get the following error:

Access token or API key not found in request parameters

I know I must provide the secret authentification key. But how do I include that in the function?

You can pass a second parameter to UrlFetchApp.fetch() for params. Inside those params you can pass things such as your method and headers - which is where you would pass your Authorization header.

Depending on your params, your code would probably look something like:

var headers = {
    Authorization: 'Bearer ' + accessToken
};  
var params = {
  'method': 'GET',
  'headers': headers,
  'contentType': 'application/json'
}
var response = UrlFetchApp.fetch(url, params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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