使用Google Apps脚本解决Mashape API问题

时间:2014-07-11 15:24:42

标签: api google-apps-script payload mashape

我现在已经使用Google Apps脚本了一段时间,但有些人总是挂断这个有效负载的事情。我只是想对mashape做一个基本的api调用。由于这是一个帖子,我很确定我应该在参数选项中使用有效负载,但是我不确定是什么给我带来了我的错误。这是我的代码:

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

}

这是它给我的错误:

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 个答案:

答案 0 :(得分:1)

我在Mashape工作(免责声明),我查看了你的代码,这是标题的问题 - 这是一个工作片段!

一切顺利,

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
}