0

I am trying to pass multiple scopes to one token in order to have to enter the URL that I was redirected only once. I have created a class Spotify which inside there I initialize the SpotifyOAuth object

class Spotify():
"""Spotify class that contains plenty of methods to handle your user or retrieve data"""

def __init__(self, cid, secret) -> None:

    self.cid = cid
    self. Secret = secret

    scope = 'user-read-private user-read-recently-played'     

    # Get the token so we don't need to access every time
    token = util.prompt_for_user_token(scope, client_id=self.cid, client_secret=self. Secret)
    sp = spotipy.Spotify(auth=token)
    
    self. Username, self.followers, self.id = try_to_login(sp)      
    self.sp = sp

The scope for user-read-private seems to work well but when I am trying to call self.sp.current_user_recently_played(limit=50) I get this error:

 return self._get(
File "C:\Users\kyria\AppData\Roaming\Python\Python39\site-packages\spotipy\client.py", line 321, in _get
 return self._internal_call("GET", url, payload, kwargs)
File "C:\Users\kyria\AppData\Roaming\Python\Python39\site-packages\spotipy\client.py", line 291, in _internal_call
 raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/me/player/recently-played?limit=50:
Insufficient client scope, reason: None

I tried to define the scope according to here as you can see in the code and also in form of scope = 'user-read-private,user-read-recently-played' and scope = ['user-read-private', 'user-read-recently-played'] according to the official documentation.

How can I achieve of working with multiple scopes at one SpotifyOAuth object if none of these 3 work?

1 Answers1

0

The Get Recently Played Tracks API on Spotify specifically deals with user-related data. The information you are trying to access is tied to a user's account, and therefore, it is essential to have the user's authorization to access their recently played tracks.

You need to have two conditions

#1 Authorized Code Flow + #2 user-read-recently-played Scope

This diagram shows what is different from Spotify documentation.

Authorization Code Flow vs. Client Credentials Flow

403 error is an Insufficient client scope error. enter image description here

I think you miss either one, maybe missed the scope or not the user login required code flow step.

enter image description here

This python code will work using spotipy v 2.22.1

Save as get-songs.py

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os

# Set your Spotify API credentials as environment variables, it will pick by spotipy API
os.environ["SPOTIPY_CLIENT_ID"] = "<your client ID>"
os.environ["SPOTIPY_CLIENT_SECRET"] = "<your client Secret>"
os.environ["SPOTIPY_REDIRECT_URI"] = "<your App's registered redirect URI>"

# Just this scope good enough to get user_recently_played API
scope = "user-read-recently-played"

# This API call raise web browser and ask user login for getting callback with Authorization Code flow
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))

# After get token, can call real API
recently_played = sp.current_user_recently_played()

# from the result, we picked up the songs track information
for idx, item in enumerate(recently_played["items"]):
    track = item["track"]
    print(idx, track['artists'][0]['name'], " – ", track['name'])

Install dependency

pip install spotipy

Run it

python get-songs.py

User Login Steps enter image description here

Result enter image description here

If you want to using Flaks & requests (low level) instead of spotipy (high level) in here

This Client Credentials Flow example

Bench Vue
  • 5,257
  • 2
  • 10
  • 14