[英]Authorization error with Google apps script
I'm not experienced with Google Apps script.我没有使用 Google Apps 脚本的经验。 I need to communicate with an external API but keep getting {"error":"Authentication failed"} response.我需要与外部 API 通信,但不断收到 {"error":"Authentication failed"} 响应。
I appreciate if someone could give me a hint what should I change in my code.如果有人能给我提示我应该在我的代码中更改什么,我将不胜感激。 I think that I have written the Authorization part wrong (in API documentation is written: Authorization: WebpageToken realm="api", apikey="testkey123")我认为我写错了授权部分(在 API 文档中写道:授权:WebpageToken realm="api", apikey="testkey123")
Below is the API documentation which I'm trying to get to work.下面是我正在尝试使用的 API 文档。
“API-documentation: Some interface functions: commitments, employees, corporates, customers/:id “API 文档:一些接口功能:承诺、员工、公司、客户/:id
API communication is done by RESTful-type HTTPS requests which are sent to: https://the.webpage.com/ {CUSTOMER_ID}/api/{ENDPOINT} Where {CUSTOMER_ID} is personal ID and {ENDPOINT} is interface function. API communication is done by RESTful-type HTTPS requests which are sent to: https://the.webpage.com/ {CUSTOMER_ID}/api/{ENDPOINT} Where {CUSTOMER_ID} is personal ID and {ENDPOINT} is interface function. The response format is JSON.响应格式为 JSON。
Example request (from documentation):示例请求(来自文档):
GET /1200/api/customers/1 HTTP/1.1
Host: the.webpage.com
Accept: application/json
Authorization: WebpageToken realm="api", apikey="testkey123"
” ”
I have tried to find correct way of writing the authorization by searching from stackoverflow and documentation but with bad luck.我试图通过从 stackoverflow 和文档中搜索来找到编写授权的正确方法,但运气不好。
MY CODE in Google Apps Script:我在 Google Apps 脚本中的代码:
function getCorporates(){
var ENDPOINT = "corporates";
var apiKey = ”testkey123”; //example
var CUSTOMER_ID = 1234; //example
var options = {
"method":"GET",
"muteHttpExceptions": true,
"Authorization":{
"WebpageToken":{
"realm":"api",
"apikey" :apiKey
}
}
};
var url = "https://the.webpage.com/" + CUSTOMER_ID + "/api/" + ENDPOINT;
var response = UrlFetchApp.fetch(url, options); // get api endpoint
var json = response.getContentText(); // get the response content as text
Logger.log(json); //log data to logger to check
}
This code should return a JSON object.此代码应返回 JSON object。 Currently I'm receiving {"error":"Authentication failed"} response.目前我收到 {"error":"Authentication failed"} 响应。
Please note that I had to "hide" the company name so in this case it's changed to the.webpage.com which is not the real API address which I try to call.请注意,我必须“隐藏”公司名称,因此在这种情况下,它已更改为 the.webpage.com,这不是我尝试调用的真正 API 地址。
Thanks for help!感谢帮助!
You need to add the Authorization
as a header , which is done like so:您需要将Authorization
添加为header ,如下所示:
var headers = {
"Authorization" : "Webpage realm='api', apikey='testkey123'"
};
var params = {
"method": "GET",
"muteHttpExceptions": true,
"headers": headers
};
var response = UrlFetchApp.fetch(url, params);
In other words the HTTP headers are their own object within the params
object (from my example, options
from your example).换句话说,HTTP 标头是它们自己的 object params
object (来自我的示例,来自您的示例的options
)。
See the documentation [0] under 'Advanced Parameters'请参阅“高级参数”下的文档 [0]
[0] https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object) [0] https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.