针对REST API的授权会引发错误:401(未找到请求的凭据.) [英] Authorization against REST API throws an error: 401 (No credentials found the request.)
问题描述
我想针对HP Alm Rest API进行授权,我认为这种应该"有效,但事实并非如此:
I want to authorize against the HP Alm Rest API, I think this "should" work, but it does not:
function performSignIn(){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password));
fetch(sign_in, {mode: 'no-cors', method:'POST', headers: headers})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
我收到一些奇怪的错误消息:
I get a bit weird error message:
401(没有凭据找到请求.)
这是HP文档中使用XMLHttpRequest完成的示例: http://alm-help.saas.hpe.com/de/12.53/api_refs/REST/webframe.htm#signin-out_example.htm
This is an example from the HP documentation, done with XMLHttpRequest: http://alm-help.saas.hpe.com/de/12.53/api_refs/REST/webframe.htm#signin-out_example.htm
有什么主意吗?我看不出有什么问题.
Any idea? I don't see what's wrong.
非常感谢!
最诚挚的问候,丹尼尔
推荐答案
fetch(...)
默认情况下不会发送凭据.您需要明确指定应发送:
fetch(…)
by default doesn’t send credentials. You need to explicitly specify they should be sent:
fetch(sign_in, {credentials: 'include' , method:'POST', headers: headers})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
此外,顺便说一句,您绝对不想指定 mode:'no-cors'
.这样做的结果是告诉浏览器阻止我的前端JavaScript访问此请求的响应.
Also, incidentally, you definitely don’t want to specify mode: 'no-cors'
. The effect that has is to tell the browser, Block my frontend JavaScript from accessing the response from this request.
这篇关于针对REST API的授权会引发错误:401(未找到请求的凭据.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!