简体   繁体   English

对 Spotify API 的 POST 请求说没有令牌

[英]POST request to Spotify API saying no token

I am getting this error message when I am trying a POST request:我在尝试 POST 请求时收到此错误消息:

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

The thing is I am passing in an access token by making a headers variable and assigning it to the headers for the post request like this:问题是我通过创建一个 headers 变量并将其分配给 post 请求的 headers 来传递访问令牌,如下所示:

const headers = {
            Authorization: `Bearer ${accessToken}`
        }
return fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, {
                'headers': headers,
                method: 'POST',
                body: JSON.stringify({'name': name})

This fetch request is the one giving me the error I stated above.这个获取请求是给我上面提到的错误的请求。 How can I fix this?我怎样才能解决这个问题?

Headers object picture right before the fetch request获取请求之前的标头对象图片

Here is the link to the GitHub: https://github.com/aoljaca/jamming这是 GitHub 的链接: https : //github.com/aoljaca/jamming

This code is in the src/utils/Spotify document此代码在 src/utils/Spotify 文档中

Thanks!谢谢!

I tried running your code but it threw a different error before it it was able to reach that part of your program.我尝试运行你的代码,但在它能够到达你程序的那部分之前它抛出了一个不同的错误。

One possible debugging strategy is making sure the headers variable is still defined and includes the access key before returning the fetch request.一种可能的调试策略是确保在返回获取请求之前仍定义headers变量并包含访问密钥。 Just print it to console before running that particular fetch.只需在运行该特定提取之前将其打印到控制台即可。 If it goes out of scope at some point in your promise chain then it could be null .如果它在您的承诺链中的某个点超出范围,则它可能为null

I'd also suggest using headers: headers instead of 'headers': headers .我还建议使用headers: headers而不是'headers': headers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM