2014-07-11 93 views
0

我一直在使用Google Apps腳本一段時間,但一些人總是掛在這個有效載荷的東西上。我只是想對mashape做一個基本的API調用。由於這是一個post調用,我很確定我應該在參數選項中使用有效載荷,但只是不確定是什麼把我的錯誤拋給了我。這裏是我的代碼:使用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

我在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 
} 
+0

就像一個魅力!非常感謝 – mikemccarthys