User authenication with python

 

I tried to make a user authentication using python scripts. 

First I retrieve the auth_headers:

auth_headers = {
"client_id": client_id,
"response_type": "code",
"redirect_uri": "http://localhost:7777/callback",
"scope": "user-library-read"
}

r = webbrowser.open("https://accounts.spotify.com/authorize?" + urlencode(auth_headers))

 

Then I get the code from the callback url "code=..."

code = "...."

encoded_credentials = base64.b64encode(client_id.encode() + b':' + client_secret.encode()).decode("utf-8")

token_headers = {
"Authorization": "Basic " + encoded_credentials,
"Content-Type": "application/x-www-form-urlencoded"
}

token_data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "http://localhost:7777/callback"
}

r = requests.post("https://accounts.spotify.com/api/token", data=token_data, headers=token_headers)

 

 

So my problem is I have to run the first script to get the authorization code whenever I refresh. Is there anyway to automate this process? Thank you so much

Child Item
Child Item
Child Item

Accepted answers

Okay, maybe this answer on Stack Overflow can help you?

All comments

With the Python module SpotiPy (GitHub | Documentation).

Thank you for your suggestion. I have already used it for a while, but I am trying to write my own request using plain python for studying purpose so I am looking for a pure python way instead.

Okay, maybe this answer on Stack Overflow can help you?

I think this is what im looking for, thank you so much

Child Item
Child Item