0

I've been using Google Apps Script for a little while now, but some how always get hung up on this payload thing. I'm just trying to to do a basic api call to mashape. Since it is a post call I'm pretty sure I should use the payload in the parameter-options, but just not really sure what's throwing me my error. Here is my code:

function mashapeTextSentiment(text){
  var key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  var url = "https://japerk-text-processing.p.mashape.com/sentiment/";
  var language = "english";

  var payload = {
    "X-Mashape-Key": key,
    "language": language,
    "text": text
  };

  var options = {
    "method": "post",
    "payload": payload
  };

  var response = UrlFetchApp.fetch(url, options);
  var rs = JSON.parse(response.getContentText());

  return rs;
}

function testMashapeTextSentiment(){

  Logger.log(mashapeTextSentiment("Someone please help me with this!"));

}

And this is the error it is giving me:

Request failed for https://japerk-text-processing.p.mashape.com/sentiment/ returned code 401. Truncated server response: {"message":"Invalid Mashape application key provided"} (use muteHttpExceptions option to examine full response) (line 17, file "Code")

1 Answers1

1

I work at Mashape (disclaimer), I looked at ur code, it was a problem with the header - here's a working snippet!

All the best,

function mashape_all_things() {


var url = "https://japerk-text-processing.p.mashape.com/sentiment/";
  var language = "english";
  var text = "mashape's orlie is the great overlord"
  var payload = {
    "language": language,
    "text": text
  };

  var options = {
    "method": "post",
    "headers": { //this is where you went wrong, you didnt pass the header properly
      "X-Mashape-Key": "XXXXXXXXXXXXXXXXXX"
    },
    "payload": payload
  };

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
  return
}
API_sheriff_orlie
  • 1,223
  • 10
  • 18