So, I'm trying to build a Spotify API Authorization Code Flow from scratch and I have the following code:
class SpotifyAPIFlow(object):
access_token = None
access_token_expires = datetime.datetime.now()
access_token_did_expire = True
client_id = None
client_secret = None
token_url = "https://accounts.spotify.com/api/token"
auth_url = "https://accounts.spotify.com/authorize"
def __init__(self, client_id, client_secret, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = client_id
self.client_secret = client_secret
def get_client_credentials(self):
"""
:return: a base 64 encoded string
"""
client_id = self.client_id
client_secret = self.client_secret
if client_secret is None or client_id is None:
raise Exception("You must set client id and client secret!\n")
client_creds = f"{client_id}:{client_secret}"
client_creds_b64 = base64.b64encode(client_creds.encode())
return client_creds_b64.decode()
def get_token_headers(self):
client_creds_b64 = self.get_client_credentials()
return {"Authorization": f"Basic {client_creds_b64}"}
def get_token_data(self):
return {"grant_type": "authorization_code"}
def perform_authentication(self):
auth_url = self.auth_url
headers = {'client_id': self.client_id, 'response_type': 'code',
'redirect_uri': 'https://open.spotify.com/collection/playlists', 'scope': 'user-top-read'}
auth_code = requests.get(auth_url, headers)
print(auth_code.headers['set-cookie'])
token_headers = self.get_token_headers()
payload = {
'grant_type': 'authorization_code',
'code': auth_code,
'redirect_uri': 'https://open.spotify.com/collection/playlists',
}
#
access_token_request = requests.post(url=self.token_url, data=payload, headers=token_headers)
access_token_response_data = access_token_request.json()
print(access_token_response_data)
# access_token = access_token_response_data['access_token']
To the first request in the perform_authentication function, I get a response with the status 200, which as far as I'm aware means it's all good, but what I found to be real sketchy is that after requesting it didn't redirect me at all to any site, for whatever reason, and they didn't even make me accept the scopes (I'm using pycharm) and I know the auth code should be something related to the url it redirects me to, but it doesn't redirect me to anything
As I still quite am a starter at this stuff, I'd appreciate any help as to why it doesn't work as I want it to, thanks.