Spotify API 在请求用户的热门歌曲时返回错误代码 403。

huangapple go评论66阅读模式
英文:

Spotify API responding with error code 403 when requesting top songs of a user

问题

I'm here to help with the translation. Here's the translated code:

我一直在编写一个程序用来使用Spotify API获取用户的热门歌曲下面是代码示例

def get_token():
    auth_string = CLIENT_ID + ":" + CLIENT_SECRET
    auth_bytes = auth_string.encode("utf-8")
    auth_base64 = str(base64.b64encode(auth_bytes), "utf-8")

    url = "https://accounts.spotify.com/api/token"
    headers = {
        "Authorization" : "Basic " + auth_base64,
        "Content-Type" : "application/x-www-form-urlencoded"
    } 

    data = {"grant_type" : "client_credentials"}
    result = post(url, headers=headers, data=data)
    json_result = json.loads(result.content)
    token = json_result["access_token"]
    return json_result

def get_top_tracks(token):
    query_url = "https://api.spotify.com/v1/me/top/tracks"
    header = {
        "Authorization" : "Bearer " + token
    }
    result = get(query_url, headers=header)
    print(result)

token = get_token()
get_top_tracks(myToken)

The error message you're encountering, <Response [403]>, indicates a 403 Forbidden response from the server. You may want to check your authentication credentials and ensure that you have the necessary permissions to access the Spotify API.

英文:

I've been writing a program to fetch the top songs of a user using Spotify API. The code is mentioned below:

def get_token():
    auth_string = CLIENT_ID + ":" + CLIENT_SECRET
    auth_bytes = auth_string.encode("utf-8")
    auth_base64 = str(base64.b64encode(auth_bytes), "utf-8")

    url = "https://accounts.spotify.com/api/token"
    headers = {
        "Authorization" : "Basic " + auth_base64,
        "Content-Type" : "application/x-www-form-urlencoded"
    } 

    data = {"grant_type" : "client_credentials"}
    result = post(url, headers=headers, data=data)
    json_result = json.loads(result.content)
    token = json_result["access_token"]
    return json_result

def get_top_tracks(token):
    query_url = "https://api.spotify.com/v1/me/top/tracks"
    header = {
        "Authorization" : "Bearer " + token
    }
    result = get(query_url, headers=header)
    print(result)

token = get_token()
get_top_tracks(myToken)

I'm getting the following error on execution:

<Response [403]>

Am I missing something?

答案1

得分: 1

Your code looks correct, but you are requesting the wrong type of token for your desired api endpoint. Any user related calls require a user token via a correctly scoped authorization code. https://developer.spotify.com/documentation/web-api/tutorials/code-flow

英文:

Your code looks correct, but you are requesting the wrong type of token for your desired api endpoint. Any user related calls require a user token via a correctly scoped authorization code. https://developer.spotify.com/documentation/web-api/tutorials/code-flow

huangapple
  • 本文由 发表于 2023年7月13日 12:17:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定