【问题标题】:Access API tokens using Fetch使用 Fetch 访问 API 令牌
【发布时间】:2021-05-15 03:23:53
【问题描述】:

我在使用 API 方面完全是初学者,但我正在尝试使用令牌端点将我的秘密交换为 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);

});

我收到此错误:“token {error: "invalid_request", error_description: "grant_type missing"}"

【问题讨论】:

  • 试试body: JSON.stringify({grant_type: client_credentials, client_id: key})
  • 出现同样的错误
  • 是的,我没有注意到标题错误

标签: javascript api fetch


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 2017-02-13
    • 1970-01-01
    • 2019-04-11
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2016-12-18
    相关资源
    最近更新 更多