首页 > 解决方案 > 使用 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”}”

标签: javascriptapifetch

解决方案


请尝试以下代码来更正您的标题部分

 headers: {
    "Content-Type": "application/json",
    "Authorization":"Bearer [value of JWT Token]"}
  • 我假设您将 JSON 作为内容传递。
  • 此外,我将 JWT 授权令牌传递给授权密钥。

让我知道这是否适合你。谢谢。


推荐阅读