0

I'm following the spotify documentation to authenticate through the WEB API .... with the code below I can get the access authorization

import requests
endpoint_auth = 'https://accounts.spotify.com/authorize'
redirect_uri = 'http://localhost:8888/spotify/index.html'
client_id = '1234567890'
scope = "playlist-modify-private"
params_auth = {
 "response_type": "code",
 "client_id": client_id,
 "scope": scope,
 "redirect_uri": redirect_uri,
}
response_auth = requests.get(url=endpoint_auth, params=params_auth)
print(response_auth.status_code)
print(response_auth.text)

first print is all ok response 200 the second print give me the content of page html for login if i save the content inside a doc html and open the file give an error but if I compose the url path and launch it from the browser like "https://accounts.spotify.com/authorize?response_type=code&client_id=1234567890&redirect_uri=http://localhost:8888/spotify/index.html&scope=playlist-modify-private" the response send me in my callback page (http://localhost:8888/spotify/index.html?code=23u2344u123u4u1) and add a CODE parameter to the url in get.... this is a parameter to request the access token.

My question is: There is a possibility to read the url callback after I have made the request so like i print response_auth.text print the callback url with parameter code?

asked May 9, 2022 at 12:41

1 Answer 1

0

You can use the requests_oauthlib Library which manages it, see https://requests-oauthlib.readthedocs.io/en/latest/examples/spotify.html

#First set the client_id/client_secret/redirect_uri
#authorization_base_url, token_url & scope
from requests_oauthlib import OAuth2Session
spotify = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
# Redirect user to Spotify for authorization
authorization_url, state = spotify.authorization_url(authorization_base_url)
print('Please go here and authorize: ', authorization_url)
# Get the authorization verifier code from the callback url
redirect_response = input('\n\nPaste the full redirect URL here: ')
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth(client_id, client_secret)
# Fetch the access token
token = spotify.fetch_token(token_url, auth=auth,
authorization_response=redirect_response)
print(token)
# Fetch a protected resource, i.e. user profile
r = spotify.get('https://api.spotify.com/v1/me')
print(r.content)
answered May 29, 2022 at 13:15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.