{;

发布于 2025-01-21 02:45:20 字数 926 浏览 4 评论 0原文

使用 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 参数丢失”}<​​/p>

为什么会失败?

Using Spotify Documentation for Client Credential Flow
(https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/)

I was able to create an API request in JavaScript:

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
        
    })
  }

But I'm receiving the following error:

{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"}

Why is this failing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2025-01-28 02:45:20

检查获取库文档,您必须通过身体场将FormData传递。
https://developer.mozilla.mozilla.org/en-us/docs/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
        
    })

Check the fetch library docs, you have to pass the formdata through body field.
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
        
    })

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文