试图从Restful API(带有React-native的Axios)获取访问令牌,但只得到错误消息:
"error":"unauthorized","errordescription":"完全身份验证需要访问此资源">
function accessToken(){
axios({
method: 'post',
url: 'myurl',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//'Access-Control-Allow-Origin': '*',
},
data: {
client_id: 'myid',
client_secret: 'mysecret',
grant_type: 'client_credentials'
},
})
.then(function(res){
console.log(res);
console.log(res.data);
})
.catch(function(err) {
console.log(err);
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
console.log(err.request);
console.log(err.message);
console.log(err.config);
});
}
Object {
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource",
}
401
我试图从由Oauth制成的Restful API中获取accesstoken。然而,我只得到错误401与
"访问此资源需要完全身份验证"。
我已经尝试过
add 'Access-Control-Allow-Origin': '*' in headers,
base64 encode id and pw,
add withCredential : true and false,
create instance with axios.create.
以下是API的使用示例。我在React native中用Axios制作的其他函数都能很好地工作,但只有这个本应获得accesstoken的函数不起作用。
curl -X POST
http://api_domain:port/oauth/token
-H 'Cache-Control: no-cache'
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'client_id=00002&client_secret=gateway-api-secret&grant_type=client_credentials'
提前谢谢。
您必须在类似的auth头中指定用户名和密码
method: "post",
url: `myurl`,
data: data,
auth: {
username: "myid",
password: "mysecret"
}
数据将有额外的字段,如grant_type等。