node.js - 在 React-native 应用程序中使用 Axios 访问此资源错误消息需要完全身份验证
问题描述
尝试从 Restful API(带有 React-native 的 Axios)获取 accesstoken,但是,只得到错误消息:
"error": "unauthorized","error_description": "访问此资源需要完全身份验证"
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 获取访问令牌。但是我只得到错误 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 等。
推荐阅读
- tinymce-4 - 小小; 表情符号不以 img 格式保存到 DB
- python - 如何在 android buildozer(kivy 框架)中安装 Android 软件包?
- r - 如何修复我的自定义函数以处理包含非数字变量的数据帧
- caching - ARM Cortex M7:缓存清理可以覆盖 DMA 设备所做的更改吗?
- python - 在 Heroku 中部署一个极其简单的 Flask 应用程序时内存不足
- python - Python - 使用 TF-IDF 汇总数据框文本列
- javascript - 尽管 memory() 显示 0 字节数,但 Tensorflow.js 内存泄漏?
- c# - 为什么我的 HttpPost 方法没有从我的 Asp.Net Core Razor 页面触发?
- jsonschema - Json Schema对外部字段的依赖
- express - 无法使用 Twilio 试用帐户接收 SMS 消息