Reputation: 3730
I want to play all the songs of a given playlist using the spotify API in python. I have got the access token and the required stuff, but am getting a response of 403, the first method find_songs
returns my playlist and it returns a response of 200, but not the second method
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 command
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
get request to get the code for curl command:
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
expired access token:
BQDbYxNY8h2Crof2_aE1-Tzgk0fnQ5EHBjenqM1dmExNpmbEaw5Ra548kEM86vSLeZ9XTuHLgRXRxxgLQF1aXcqcpUivuaEBjQSmjRFfeBGazCdVxSZQgCMDR_XRa5PhhrVebE2gYfDQl468BkSEGSGWV5LJ2EOYbWS5fsCTHWt-XtM7TFEDQJw3J43yiYf98pBWggp7CQM_yv31XLjoNe93A-j-rwQEJQ0
Upvotes: 1
Views: 760
Reputation: 3730
I figured out that the problem was not with my code, For some calls such as play/pause, increase or decrease volume require a Spotify premium account.
Upvotes: 0
Reputation: 429
I believe you are trying to hit this API. If you check the Authorization Scopes Guide, it states that you need to include the required "scope" while you are requesting the access token.
This is how most Oauth systems work. To access a protected resource, you first get an access token. But you also state that you want to do say, read on a resource XYZ. The access token that you get will have restricted permissions to do just what you wanted to do ( even though you as a user have permissions to a lot of other things ).
Scroll down to the scope that you need, and include this scope while requesting the access token. I believe this can solve your issue.
Well, how did I know how to search for this scope? I looked up the URI you are trying to hit, and looked up the scopes for that URI.
Upvotes: 1