I'm using a python wrapper for the Spotify API.
https://spotipy.readthedocs.io/en/latest/#installation
As part of the authorization process, the Spotify API has the user login to Spotify in their default web browser, then sends them to a predefined (when you register the app with Spotify) REDIRECT_URI. This REDIRECT_URI is currently set to localhost:6969. Once you login, it spawns an input() (Input of Death) for you to copy paste the URI that you were redirected to into the command prompt.
I really don't want to sell random people on using a command prompt.
The goal is to open a flask server on localhost:6969 (sue me) and then double tap its /authorize page (send to oauth2 login then capture code)
http://flask.pocoo.org/docs/1.0/
So -
I keep my Flask rig and spotify catcher in jflask.py:
from flask import Flask, request, redirect, session
from requests_oauth2 import OAuth2
from spotipy import Spotify
import spotipy.util as util
import requests
import datetime
app = Flask(__name__)
spotify_auth = OAuth2(
client_id='e33c6fa0d6a249ccafa232a9cf62a616',
client_secret='a43b0b6a4de14b97b4e468459e0f7824',
redirect_uri="http://localhost:6969/authorize",
site="https://accounts.spotify.com",
authorization_url="/authorize",
token_url="/api/token",
scope_sep=" "
)
# and then use this url as a link within a public login view
print(spotify_auth.authorize_url(
scope=["user-read-recently-played", "user-top-read"],
response_type="code"
)
)
# and have an authorize route that can direct you to Spotify or handle you
#being redirected back here from Spotify
@app .route('/authorize')
def authorize():
print('triggered')
error = request.args.get("error")
if error:
abort(404)
code = request.args.get("code")
if code:
data = spotify_auth.get_token(
code=code,
grant_type="authorization_code",
)
access_token = data["access_token"]
refresh_token = data["refresh_token"]
scopes = data["scope"].split()
self.sp = spotipy.Spotify(auth=access_token)
print(access_token)
return self.sp
else:
print('code aint defined hoe')
return redirect(spotify_auth.authorize_url(
scope=["user-read-recently-played", "user-top-read"],
response_type="code",
#state=session['state']
))
if __name__ == '__main__':
app.run(host='localhost', port=6969)
I spawn it with master.py:
from subprocess import Popen, PIPE
import webbrowser
jflask=Popen(['python', 'jflask.py'])
webbrowser.open('http://localhost:6969/authorize')
I've already tried using Popen with stdout=PIPE. After further research, I'm pretty sure that was also inhibiting my success. The specification is meant to be used with subprocess.communicate()
https://docs.python.org/3/library/subprocess.html#subprocess.PIP
Thank you everyone for your help.