【问题标题】:{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"} for Spotify API{\"error\":\"unsupported_grant_type\",\"error_description\":\"grant_type parameter is missing\"} 对于 Spotify API
【发布时间】:2022-12-22 07:04:54
【问题描述】:

将 Spotify 文档用于客户端凭证流 (https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/)

我能够在 javascript 中创建 API 请求:

function getoAuth () {
    const client_id = id;
    const client_secret = secret;
    fetch("https://accounts.spotify.com/api/token", {
        method: 'POST',
        headers: {
           'Content-type': 'application/x-www-form-urlencoded',
           'Authorization': 'Basic' + (client_id + ":" + client_secret).toString('base64')
        },
        form: {
            grant_type: 'client_credentials',
        },
        json: true
        
    })
  }

但我收到以下错误:{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"}

为什么会失败?

【问题讨论】:

    标签: javascript node.js api fetch spotify-app


    【解决方案1】:

    检查获取库文档,您必须通过正文字段传递表单数据。 https://developer.mozilla.org/en-US/docs/web/api/fetch

     fetch("https://accounts.spotify.com/api/token", {
            method: 'POST',
            headers: {
               'Content-type': 'application/x-www-form-urlencoded',
               'Authorization': 'Basic<>'
            },
            body: new URLSearchParams({
                'grant_type': 'client_credentials'
            }),
            json: true
            
        })

    【讨论】:

    • 您好,感谢您的帮助!我更新了我的代码并运行了它,但我仍然遇到同样的错误
    【解决方案2】:

    我不得不将 client_idclient_secret 放在正文中,而不是放在 Authorization 标头中。

    try {
        const body = {
          grant_type: "client_credentials",
          client_id: <YOUR_ID>,
          client_secret: <YOUR_SECRET>,
        };
    
        const response = await fetch("https://accounts.spotify.com/api/token", {
          method: "POST",
          headers: {
            "Content-type": "application/x-www-form-urlencoded",
          },
          body: new URLSearchParams(body),
        });
        console.log({ response });
      } catch (err) {
        console.log({ err });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-21
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 2021-05-02
      • 1970-01-01
      相关资源
      最近更新 更多