Google App Script UrlFetchApp.fetch API Key Header:出现 401 错误 [英] Google App Script UrlFetchApp.fetch API Key Header: getting 401 error
问题描述
我有一个 Infobip 测试帐户,我正在尝试使用他们的 API 向用户发送消息.作为授权类型,我使用 API 密钥头.我相信我不知道把这个键放在代码上的什么地方.我正在尝试使用标题上的密钥,但我总是收到错误 401.我知道我没有根据文档使用正确的有效负载参数,但无论如何,我相信这不是生成此错误的原因.
I have an Infobip test account and I am trying to use send a message to a user using their API. As an authorization type, I am using the API Key Header. I believe I don't know where to place this key on the code. I am trying to use the key on the header but I am always getting the error 401. I know that I am not using the correct payload parameters according to the documentation but anyway, I believe this is not the reason to generate this error.
function myFunction_infobip() {
var url = "https://*base URL given*.api.infobip.com/omni/1/advanced";
var authHeader = '';
var payload = JSON.stringify({
"scenarioKey":"AD9E01A5DC7BEE2C2B828D208182A611",
"whatsApp":{"text":"test."},});
var headers = {
"Authorization": "App <API key given>",
"Content-Type": "application/json",
"Accept": "application/json"};
var options = {
"method": "POST",
"contentType": "application/json",
"headers": headers,
"payload": payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log("response: "+response);
}
如果您有任何建议/线索,我将不胜感激!
I would be very grateful for any advice/clue here!
推荐答案
来自 OP 的回答从问题中移出:
对于有同样问题的人,经过多次尝试,我找到了解决方案.我也刚刚在有效负载中添加了 URL,现在它可以完美运行:
For those with the same problem, after a lot of tries, I found the solution. I just added the URL inside the payload also and it works perfectly now:
function myFunction_infobip() {
var url = "https://*base URL given*.api.infobip.com/omni/1/advanced";
var authHeader = '';
var payload = JSON.stringify({
"url": "https://*base URL given*.api.infobip.com/omni/1/advanced",
"scenarioKey":"AD9E01A5DC7BEE2C2B828D208182A611",
"WhatsApp":<insert here the parameters>);
var headers = {
"Authorization": "App <API key given>",
"Content-Type": "application/json",
"Accept": "application/json"};
var options = {
"method": "POST",
"contentType": "application/json",
"headers": headers,
"payload": payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log("response: "+response);
}
这篇关于Google App Script UrlFetchApp.fetch API Key Header:出现 401 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!