0
votes

I am trying to make a call to external api using google Appscript. But i am gettting always error 500.It would be grateful if anyone can help.

Request failed for https://demo.overconline.com/api/authenticate returned code 500. Truncated server response: {"message":"error.internalServerError","description":"Internal server error","fieldErrors":null} (use muteHttpExceptions option to examine full response) (line 10, file "Code")

  function getToken(){
  var params ={
    "username": "xxxx",
    "rememberMe": true,
    "password": "xxx",
  };  

  var headers = {'Content-Type':'application/json','method':'post','payload':'params'};
  var url = 'https://demo.overconline.com/api/authenticate';
  var response = UrlFetchApp.fetch(url,headers);
  var result = Utilities.jsonParse(response.getContentText());
  var token = result.access_token;
  Logger.log(result);
1
When headers is used as UrlFetchApp.fetch(url,headers), 'Content-Type':'application/json' can be modified as 'contentType':'application/json'. params is not used to payload as an object, because 'params' is enclosed by the single quotes. From the content type, payload might be JSON.stringify(params). Reference is here. If these modifications were not your solution, can you provide a document for the specification of the API you want to use?Tanaike
Then, Utilities.jsonParse(jsonString) had already been deprecated . So please use JSON.parse(jsonString).Tanaike
Thank you very much. It is worked now.Tharaka
Thank you for replying. I'm glad your issue was solved. Can you post an answer with your solution? When you posted an answer, please accept it. By this, other users can see the solution for your issue.Tanaike
yes i will do it for sureTharaka

1 Answers

0
votes

This is the correct code for above code.I got the authentication token by this code.

function getToken(){
  var params ={
    'username': 'xxxxx',
    'rememberMe': true,
    'password':'xxxxx'
  };  
  var headers = {'contentType':'application/json','method':'post','payload':JSON.stringify(params)};
  var url = 'https://demo.overconline.com/api/authenticate';
  var response = UrlFetchApp.fetch(url,headers);
  var result = JSON.parse(response.getContentText());
  Logger.log(result);
}