-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.py
115 lines (92 loc) · 3.77 KB
/
authorization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
from flask import Flask, request, redirect, g, render_template
import requests
import base64
import urllib
from data_scrape import readTracks
from azure_emotion import grabPlaylist
# Authentication Steps, paramaters, and responses are defined at https://developer.spotify.com/web-api/authorization-guide/
# Visit this url to see all the steps, parameters, and expected response.
app = Flask(__name__)
# Client Keys
CLIENT_ID = "3e0cc3a3afe348f8baf6c86aa83feef6"
CLIENT_SECRET = "a193a6bcd31e406e8aada3e0d5ea6eea"
# Spotify URLS
SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
SPOTIFY_API_BASE_URL = "https://api.spotify.com"
API_VERSION = "v1"
SPOTIFY_API_URL = "{}/{}".format(SPOTIFY_API_BASE_URL, API_VERSION)
# Server-side Parameters
CLIENT_SIDE_URL = "https://0.0.0.0:5000/"
PORT = ""
# REDIRECT_URI = "{}:{}/callback/q".format(CLIENT_SIDE_URL, PORT)
REDIRECT_URI = "http://0.0.0.0:5000/callback"
SCOPE = "user-read-currently-playing user-library-read"
STATE = ""
SHOW_DIALOG_bool = True
SHOW_DIALOG_str = str(SHOW_DIALOG_bool).lower()
auth_query_parameters = {
"response_type": "code",
"redirect_uri": REDIRECT_URI,
"scope": SCOPE,
# "state": STATE,
# "show_dialog": SHOW_DIALOG_str,
"client_id": CLIENT_ID
}
auth_code = ""
refresh = ""
@app.route("/")
def index():
# Auth Step 1: Authorization
url_args = "&".join(["{}={}".format(key,urllib.quote(val)) for key,val in auth_query_parameters.iteritems()])
auth_url = "{}/?{}".format(SPOTIFY_AUTH_URL, url_args)
return redirect(auth_url)
@app.route("/callback")
def callback():
# Auth Step 4: Requests refresh and access tokens
auth_token = request.args['code']
code_payload = {
"grant_type": "authorization_code",
"code": str(auth_token),
"redirect_uri": REDIRECT_URI
}
base64encoded = base64.b64encode("{}:{}".format(CLIENT_ID, CLIENT_SECRET))
headers = {"Authorization": "Basic {}".format(base64encoded)}
post_request = requests.post(SPOTIFY_TOKEN_URL, data=code_payload, headers=headers)
# Auth Step 5: Tokens are Returned to Application
response_data = json.loads(post_request.text)
access_token = response_data["access_token"]
refresh_token = response_data["refresh_token"]
token_type = response_data["token_type"]
expires_in = response_data["expires_in"]
refresh = refresh_token
auth_code = access_token
# Auth Step 6: Use the access token to access Spotify API
authorization_header = {"Authorization":"Bearer {}".format(access_token)}
# Get profile data
user_profile_api_endpoint = "{}/me".format(SPOTIFY_API_URL)
profile_response = requests.get(user_profile_api_endpoint, headers=authorization_header)
profile_data = json.loads(profile_response.text)
# Get user playlist data
playlist_api_endpoint = "{}/playlists".format(profile_data["href"])
playlists_response = requests.get(playlist_api_endpoint, headers=authorization_header)
playlist_data = json.loads(playlists_response.text)
# Combine profile and playlist data to display
display_arr = [profile_data] + playlist_data["items"]
readTracks()
grabPlaylist()
return redirect("https://adontula.github.io")
# return render_template("index.html",sorted_array=display_arr)
def refresh():
base64encoded = base64.b64encode("{}:{}".format(CLIENT_ID, CLIENT_SECRET))
headers = {"Authorization": "Basic {}".format(base64encoded)}
code_payload = {
"grant_type": "refresh_token",
"refresh_token": refresh
}
post_request = requests.post(SPOTIFY_TOKEN_URL, data=code_payload, headers=headers)
data = json.loads(post_request.text)
return data["access_token"]
if __name__ == "__main__":
app.run(debug=True,port=PORT)