javascript - 使用 Fetch 访问 API 令牌
问题描述
我是使用 API 的初学者,但我正在尝试使用 Token 端点将我的秘密交换为 JWT。(故意删除了client_credentials)
fetch('', {
method: 'POST',
body: 'grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret,
headers: {
"Content-Type": "Bearer [JWT]",
}
}).then(function (resp) {
// Return the response as JSON
return resp.json();
}).then(function (data) {
// Log the API data
console.log('token', data);
}).catch(function (err) {
// Log any errors
console.log('something went wrong', err);
});
我收到此错误:“令牌{错误:“invalid_request”,error_description:“grant_type missing”}”
解决方案
请尝试以下代码来更正您的标题部分
headers: {
"Content-Type": "application/json",
"Authorization":"Bearer [value of JWT Token]"}
- 我假设您将 JSON 作为内容传递。
- 此外,我将 JWT 授权令牌传递给授权密钥。
让我知道这是否适合你。谢谢。
推荐阅读
- c# - 用户在 C# 中删除 Excel 工作表后调用函数
- jquery - 我正在尝试使用 jquery ajax 将下拉列表绑定到 UI
- mysql - MySQL shell 无法正确处理 unicode 路径
- javascript - 如何在左右添加箭头以移动幻灯片?目前我必须按 shift 并滚动才能移动它
- spring-boot - 如何防止jms连接到服务器
- c - 使用 swig 时找不到架构 arm64 的符号
- cmd - 安装 Cassandra 时发生错误“VM 初始化期间发生错误无法为 2097152KB 对象堆保留足够的空间”
- c - 做...当变量而不是int是c中的char时,会创建无限循环
- python - 从 div 抓取数据
- typescript - 是否可以使用具有推断泛型类型的参数函数来缩小可区分联合?