Search code examples
javascriptgoogle-apps-scriptgmail-addons

Request to external api to get authentication token in google appscript


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);

Solution

  • 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);
    }