GAS: Error 401 while connecting to an API

I'm trying to connect to Cloud Waitress API, a solution for restaurants,

Documentation: https://apidocs.cloudwaitress.com/#orderpromos

It's documentation gives an example on how to connect to the API:

curl https://api.cloudwaitress.com/v1/orders \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: YOUR_API_KEY" \ 
  -d `
    {
      "restaurant_id": "xxxxxxx",
      "limit": 10,
      "page": 1,
      "sort": { "created": -1 },
    }

I tried to create a Script in GAS to get the information back to a Spreadsheet.

Based on this question:

How curl maps onto Google App Script URLFetchApp

I have changed my code as follows:

function getAuthHeader(){
  var apiKey = "SOME-API-KEY";
  var authHeader = Utilities.base64Encode(apiKey);
  return {
    headers: {Authorization: authHeader}
   }
}

function GetOrders(){  
  var url = "https://api.cloudwaitress.com/v1/orders";  
  
  var data = {
    "restaurant_id":"SOME-RESTAURANT-ID",
    "limit": 10,
    "page": 1,
    "sort": { "created": 1 }
  };
  
  var result = goPost(url ,data);

}

function goPost(url,data){
   var options = {
   'method' : 'post',
   'payload' : data,
   'headers': getAuthHeader()['headers']
   };
  
   var response; 
  
   try{
     response = JSON.parse(UrlFetchApp.fetch(url,options).getContentText());
   }catch(err){
    Logger.log(err);
   }
  
   return response;
 }

Right now the new error that I'm getting is:

Exception: Request failed for https://api.cloudwaitress.com returned code 401. Truncated server response: {"outcome":1,"message":"Invalid Authentication"} (use muteHttpExceptions option to examine full response)

Which I believe is quite a progress since I was getting an 500 error before.

enter image description here

I have asked the cloudwaitress team their assistance, however I wonder if there is something else I can try.

Upvotes: 2

Views: 337

Answers (1)

Tanaike
Tanaike

Reputation: 201378

How about this modification?

Modification points:

  • At your sample curl, "Content-Type: application/json" is used, and the JSON object is converted to the string.
  • The thread you refered uses the basic authorization like -u testtoken123:. By this, Utilities.base64Encode is required be used. But in your sample curl, the API key is directly used.
    • I thought that this might be the reason of the error message.

When above points are reflected to your script, it becomes as follows.

Modified script:

From:
var options = {
'method' : 'post',
'payload' : data,
'headers': getAuthHeader()['headers']
};
To:
var apiKey = "SOME-API-KEY";
var options = {
  'method' : 'post',
  'payload' : JSON.stringify(data),
  'headers': {Authorization: apiKey},
  'contentType': "application/json",
};
  • In this case, getAuthHeader() is not used.

Note:

  • The request of this modified script is the same with your sample curl command. But in this modified script, it supposes that your apiKey and data can be used for this API. Please be careful this.

Reference:

Upvotes: 3

Related Questions