-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_spotify.json
128 lines (91 loc) · 3.38 KB
/
rest_spotify.json
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
116
117
118
119
120
121
122
123
124
125
126
127
128
method: "POST"
url: https: //accounts.spotify.com/api/token
headers {
"Authorization": "Basic NDJlMDAxZDIzNTY1NDU1OWFiMjQ2YmQwM2NjOWE5ODY6ZDA2MjBkNTI1MDlhNDM0Njg1ZDQzZDVkNDRlMzMxZTA="
"Content-Type": "application/x-www-form-urlencoded"
}
body: {
body content type: application / x - www - form - urlencoded
encoded - payload {
"grant_type": "client_credentials"
}
}
///////////////////////////////////////////////////////////////77
import json
from flask
import Flask, request, redirect, g, render_template
import requests
import base64
import urllib
# 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 = ""
CLIENT_SECRET = ""
#
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 = "http://127.0.0.1"
PORT = 8080
REDIRECT_URI = "{}:{}/callback/q".format(CLIENT_SIDE_URL, PORT)
SCOPE = "playlist-modify-public playlist-modify-private"
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
}
@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/q")
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"]
# 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"]
return render_template("index.html", sorted_array = display_arr)
if __name__ == "__main__":
app.run(debug = True, port = PORT)
///////////////////////////////