简体   繁体   English

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

[英]How to pass api ID and api Key as authentication parameters of HTTP request

I am having trouble acessing the Oxford Dictionary API.我在访问牛津词典 API 时遇到问题。 I keep getting the error 'Authentication parameters missing'.我不断收到错误“缺少身份验证参数”。

I believe I am not properly passing the api ID and key.我相信我没有正确传递 api ID 和密钥。 I have referred to this documentation: https://developer.oxforddictionaries.com/documentation/making-requests-to-the-api https://developer.oxforddictionaries.com/documentation我参考了这个文档: https : //developer.oxforddictionaries.com/documentation/making-requests-to-the-api https://developer.oxforddictionaries.com/documentation

Here is my code;这是我的代码; trust me that the app id and key are the correct strings.相信我,应用程序 ID 和密钥是正确的字符串。

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);
});

If the site looking for Authentication header it may refer to Basic Auth Header, read more here .如果站点寻找身份验证标头,它可能会参考基本身份验证标头, 请在此处阅读更多信息 If that is true, you simply can append your header parameter with this:如果这是真的,你可以简单地附加你的头参数:

var headers = new Headers()

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

You may try with or without the encoding.您可以尝试使用或不使用编码。

You may also try this if that does not work如果这不起作用,您也可以尝试

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);
}); 

If that does not work, another way to think of, it may be a CORS issue, you can read it more here如果这不起作用,另一种思考方式,可能是 CORS 问题,您可以在此处阅读更多内容

May this helps.可能这会有所帮助。

I found an answer to my question here .我在这里找到了我的问题的答案。 Looks like the API does not support client side application requests.看起来 API 不支持客户端应用程序请求。 Guess I'll have to create a server.猜猜我必须创建一个服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM