Invalid Argument on basic API call using UrlFetchApp.fetch(url)

时间:2016-04-25 09:27:38

标签: google-apps-script

I'm new to Google Apps Scripts and I've been trying to make a simple Get call to a URL. I make this call from my browser: https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740 and get the respons I'm looking for. I now try to make the call from Google Apps Scripts using the following code:

function myFunction() {
var url = 'https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740
var response = UrlFetchApp.fetch(url);
Logger.log(response);
}'

I get a respons stating ' Message details Invalid argument: https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740 (line 4, file "Code")'

I've tried a whole bunch of permutations with no luck...

1 个答案:

答案 0 :(得分:3)

使用UrlFetchApp时,您需要输入URL参数作为请求参数的一部分,而不是URL本身。对于GET请求,这些请求作为参数的一部分,对于POST请求,参数将是有效负载对象的一部分。 Reference Documentation

这是一个修改过的功能:

function myFunction() {
  var url = 'https://accounting.sageone.co.za/api/1.1.1/Supplier/Get'  
  var params = {
    "method": 'GET',
    "apikey": "{4CFEEFE1-CE04-425F-82C3-DCB179C817D5}",
    "companyid": "164740"
  }
  var response = UrlFetchApp.fetch(url, params);
  Logger.log(response)
}

注意:这会更正您的方法,但是,服务器仍需要进一步的身份验证。如果您遇到问题,请提出另一个特定于该问题的问题。