英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论