gpt4 book ai didi

javascript - 如何将api ID和api Key作为HTTP请求的认证参数传递

转载 作者:行者123 更新时间:2023-11-29 20:57:08 25 4
gpt4 key购买 nike

我在访问 Oxford Dictionary API 时遇到问题。我不断收到错误消息“缺少身份验证参数”。

我相信我没有正确传递 api ID 和 key 。我已经引用了这个文档: https://developer.oxforddictionaries.com/documentation/making-requests-to-the-api https://developer.oxforddictionaries.com/documentation

这是我的代码;相信我,应用 ID 和 key 是正确的字符串。

const url = 'https://od-api.oxforddictionaries.com/api/v1/entries/en/swim';

fetch(url, {
method: 'GET',
headers: new Headers({
'app_id': oxford.appId, 'app_key': oxford.appKey
}),
}).then(function(response) {
console.log(response);
});

最佳答案

如果网站寻找身份验证 header ,它可能会引用基本身份验证 header ,read more here .如果是这样,您只需将 header 参数附加到此即可:

var headers = new Headers()

headers.append('Authorization', 'Basic ' + base64.encode(app_id + ":" + app_key));

您可以尝试使用或不使用编码。

不行的话你也可以试试

const url = 'https://od-api.oxforddictionaries.com/api/v1/entries/en/swim';

fetch(url, {
method: 'GET',
headers: { //HERE IS THE DIFFERENCE
'app_id': oxford.appId, 'app_key': oxford.appKey
},
mode: 'no-cors'
}).then(function(response) {
console.log(response);
});

如果不行的话换个思路,可能是CORS的问题,可以多看看here

希望这有帮助。

关于javascript - 如何将api ID和api Key作为HTTP请求的认证参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com