HTTP获取Spotify API令牌请求失败

发布于 2025-01-26 00:42:35 字数 2670 浏览 2 评论 0原文

在Spotify WebAPI文档的指导下,我试图通过客户凭证方法请求访问令牌。 spotify api doc 。我想使用常规的HTTP提取请求,我无法使用任何第三方库。我收到400个返回状态错误响应:

{error:“ unsupported_grant_type”,error_description:“ grant_type参数丢失”}。

但是,我相信我的请求应为其赠款类型正确格式化。我看过大量文章,MDN Doc和Spotify Doc,我无法弄清楚为什么这不起作用。我将包括我显然从API键中取出的代码,但它们是正确的。 链接到代码

//Basic imports
import React, { Component, useState , useEffect } from 'react';
//Custom IMPORTS: 
import '../PageCss/HeaderSection.css'



const Spotify = () => {


    const [baseUrl, setBaseUrl] = useState("https://accounts.spotify.com/api/token");
    const [token, setToken] = useState([]);
    const [currentStatus, setStatus] = useState(false);

    const client_id = '';
    const client_secret = '';
    const data = { grant_type: 'client_credentials' };

    useEffect(() => {
        fetch(baseUrl,
            {
                method: 'POST',
                headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
                  'Authorization': 'Basic ' + (client_id + ':' + client_secret).toString('base64')
                },
                redirect: 'follow',
                body: JSON.stringify(data),
            })
      .then((response) => {
        if (!response.ok) {
          return Promise.reject(new Error("Response Error!"));
        }
        else {
          return response.json();
        }
      })
      .catch((err) => {
        console.log(err);
      })
      .then((json) => {
        try {
          setToken(json.results);
          setStatus(true);
          console.log("TOKEN:" + token)

        }
        catch
        {
          return Promise.reject(new Error(`State Error!: Data: ${token} , Connection:${currentStatus}`));
        }
      })
      .catch((err) => {
        console.log(err);
      })
    }, [baseUrl]);


    return (

        <div >
             
        </div>
    )
};



export default Spotify;

我的应用程序是托管在GitHub上的React应用程序。这是一个功能齐全的站点,其他所有内容都可以正常工作。我的其他API获取电话工作正常,所以我知道这必须有问题。给我一个错误的唯一代码行是从提取请求中获得的400个状态。

Under guidance of the Spotify WebAPI doc I am trying to request an access token via Client Credentials method. Spotify API Doc. I want to use a regular HTTP fetch request, I can not use any 3rd party libraries. I am getting a 400 return status error response:

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

However, I believe my request should be formatted correctly for its grant type. I have looked at tons of articles, MDN doc, and the Spotify doc and I can not figure out why this is not working. I will include the code which I have obviously taken the API keys out of but they are correct. Link to code.

//Basic imports
import React, { Component, useState , useEffect } from 'react';
//Custom IMPORTS: 
import '../PageCss/HeaderSection.css'



const Spotify = () => {


    const [baseUrl, setBaseUrl] = useState("https://accounts.spotify.com/api/token");
    const [token, setToken] = useState([]);
    const [currentStatus, setStatus] = useState(false);

    const client_id = '';
    const client_secret = '';
    const data = { grant_type: 'client_credentials' };

    useEffect(() => {
        fetch(baseUrl,
            {
                method: 'POST',
                headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
                  'Authorization': 'Basic ' + (client_id + ':' + client_secret).toString('base64')
                },
                redirect: 'follow',
                body: JSON.stringify(data),
            })
      .then((response) => {
        if (!response.ok) {
          return Promise.reject(new Error("Response Error!"));
        }
        else {
          return response.json();
        }
      })
      .catch((err) => {
        console.log(err);
      })
      .then((json) => {
        try {
          setToken(json.results);
          setStatus(true);
          console.log("TOKEN:" + token)

        }
        catch
        {
          return Promise.reject(new Error(`State Error!: Data: ${token} , Connection:${currentStatus}`));
        }
      })
      .catch((err) => {
        console.log(err);
      })
    }, [baseUrl]);


    return (

        <div >
             
        </div>
    )
};



export default Spotify;

enter image description here
enter image description here

My application is a react app, hosted on GitHub. It's a fully functioning site and everything else is working fine. My other API fetch calls are working fine so I know this one must have an issue in it. The only line of code giving me an error is this 400 status from the fetch request.

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

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

发布评论

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

评论(1

薆情海 2025-02-02 00:42:35

嘿,所以我实际上收到了使用此代码的Inital代码请求:

fetch(baseUrl,
      {
        method: 'POST',
        body: 'grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })

受OAuth Doc的启发,还有一些研究。它现在起作用,我有Spotify的象征。

Hey so I actually got the inital token request to work with this code:

fetch(baseUrl,
      {
        method: 'POST',
        body: 'grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })

Inspired by the OAuth doc and some more researching. It works now and I have a token from Spotify.

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