如何播放指定播放列表中的歌曲 (Spotify API)
How to play a song from a specified playlist (Spotify API)
我想在 python 中使用 spotify API 播放给定播放列表中的所有歌曲。我有访问令牌和所需的东西,但得到的响应是 403,第一种方法 find_songs
returns 我的播放列表 returns 响应是 200,但第二种方法不是方法
class SaveSongs:
def __init__(self):
self.user_id = spotify_user_id
self.spotify_token = ""
self.playlist_id = playlist_id
self.tracks = ""
self.new_playlist_id = ""
self.device_id = "DESKTOP-9UBFPPN"
def find_songs(self):
print("Finding songs in your playlist...")
query = "https://api.spotify.com/v1/playlists/{}/tracks".format(
playlist_id)
response = requests.get(query,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(self.spotify_token)})
response_json = response.json()
print(response)
def call_refresh(self):
print("Refreshing token")
refreshCaller = Refresh()
self.spotify_token = refreshCaller.refresh()
self.find_songs()
def playSong(self): #issue here
query = "https://api.spotify.com/v1/me/player/play"
response = requests.put(query,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(self.spotify_token)}, data={
"context_uri": "spotify:playlist:7mnkglhHfzZRYFRG72zCMd"
})
print(response)
a = SaveSongs()
a.call_refresh()
a.playSong()
Refresh.py:
class Refresh:
def __init__(self):
self.refresh_token = refresh_token
self.base_64 = base_64
def refresh(self):
query = "https://accounts.spotify.com/api/token"
response = requests.post(query,
data={"grant_type": "refresh_token",
"refresh_token": refresh_token},
headers={"Authorization": "Basic " + base_64})
response_json = response.json()
print(response_json)
return response_json["access_token"]
a = Refresh()
a.refresh()
cURL 命令
curl -H "Authorization: Basic " -d grant_type=authorization_code -d code= -d redirect_uri=https%3A%2F%2Fgithub.com%2FRohith-JN https://accounts.spotify.com/api/token
获取获取 curl 命令代码的请求:
https://accounts.spotify.com/authorize?client_id=&response_type=code&redirect_uri=https%3A%2F%2Fgithub.com%2FRohith-JN&scope=playlist-modify-public%20playlist-modify-private%20user-modify-playback-state
过期的访问令牌:
BQDbYxNY8h2Crof2_aE1-Tzgk0fnQ5EHBjenqM1dmExNpmbEaw5Ra548kEM86vSLeZ9XTuHLgRXRxxgLQF1aXcqcpUivuaEBjQSmjRFfeBGazCdVxSZQgCMDR_XRa5PhhrVebE2gYfDQl468BkSEGSGWV5LJ2EOYbWS5fsCTHWt-XtM7TFEDQJw3J43yiYf98pBWggp7CQM_yv31XLjoNe93A-j-rwQEJQ0
我相信你是想打 this API。
如果您检查 Authorization Scopes Guide,它表明您在请求访问令牌时需要包括所需的“范围”。
这就是大多数 Oauth 系统的工作方式。要访问受保护的资源,您首先要获得访问令牌。但是您还声明您想说,阅读资源 XYZ。您获得的访问令牌将具有受限的权限,只能执行您想要执行的操作(即使您作为用户拥有许多其他操作的权限)。
向下滚动到 the scope that you need,并在请求访问令牌时包含此范围。我相信这可以解决你的问题。
嗯,我怎么知道如何搜索这个范围?我查找了您要访问的 URI,并查找了该 URI 的范围。
我发现问题不在于我的代码,对于某些调用,例如 play/pause,增加或减少音量需要 Spotify 高级帐户。
我想在 python 中使用 spotify API 播放给定播放列表中的所有歌曲。我有访问令牌和所需的东西,但得到的响应是 403,第一种方法 find_songs
returns 我的播放列表 returns 响应是 200,但第二种方法不是方法
class SaveSongs:
def __init__(self):
self.user_id = spotify_user_id
self.spotify_token = ""
self.playlist_id = playlist_id
self.tracks = ""
self.new_playlist_id = ""
self.device_id = "DESKTOP-9UBFPPN"
def find_songs(self):
print("Finding songs in your playlist...")
query = "https://api.spotify.com/v1/playlists/{}/tracks".format(
playlist_id)
response = requests.get(query,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(self.spotify_token)})
response_json = response.json()
print(response)
def call_refresh(self):
print("Refreshing token")
refreshCaller = Refresh()
self.spotify_token = refreshCaller.refresh()
self.find_songs()
def playSong(self): #issue here
query = "https://api.spotify.com/v1/me/player/play"
response = requests.put(query,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(self.spotify_token)}, data={
"context_uri": "spotify:playlist:7mnkglhHfzZRYFRG72zCMd"
})
print(response)
a = SaveSongs()
a.call_refresh()
a.playSong()
Refresh.py:
class Refresh:
def __init__(self):
self.refresh_token = refresh_token
self.base_64 = base_64
def refresh(self):
query = "https://accounts.spotify.com/api/token"
response = requests.post(query,
data={"grant_type": "refresh_token",
"refresh_token": refresh_token},
headers={"Authorization": "Basic " + base_64})
response_json = response.json()
print(response_json)
return response_json["access_token"]
a = Refresh()
a.refresh()
cURL 命令
curl -H "Authorization: Basic " -d grant_type=authorization_code -d code= -d redirect_uri=https%3A%2F%2Fgithub.com%2FRohith-JN https://accounts.spotify.com/api/token
获取获取 curl 命令代码的请求:
https://accounts.spotify.com/authorize?client_id=&response_type=code&redirect_uri=https%3A%2F%2Fgithub.com%2FRohith-JN&scope=playlist-modify-public%20playlist-modify-private%20user-modify-playback-state
过期的访问令牌:
BQDbYxNY8h2Crof2_aE1-Tzgk0fnQ5EHBjenqM1dmExNpmbEaw5Ra548kEM86vSLeZ9XTuHLgRXRxxgLQF1aXcqcpUivuaEBjQSmjRFfeBGazCdVxSZQgCMDR_XRa5PhhrVebE2gYfDQl468BkSEGSGWV5LJ2EOYbWS5fsCTHWt-XtM7TFEDQJw3J43yiYf98pBWggp7CQM_yv31XLjoNe93A-j-rwQEJQ0
我相信你是想打 this API。 如果您检查 Authorization Scopes Guide,它表明您在请求访问令牌时需要包括所需的“范围”。
这就是大多数 Oauth 系统的工作方式。要访问受保护的资源,您首先要获得访问令牌。但是您还声明您想说,阅读资源 XYZ。您获得的访问令牌将具有受限的权限,只能执行您想要执行的操作(即使您作为用户拥有许多其他操作的权限)。
向下滚动到 the scope that you need,并在请求访问令牌时包含此范围。我相信这可以解决你的问题。
嗯,我怎么知道如何搜索这个范围?我查找了您要访问的 URI,并查找了该 URI 的范围。
我发现问题不在于我的代码,对于某些调用,例如 play/pause,增加或减少音量需要 Spotify 高级帐户。