7
7
8
8
9
9
def authorize (auth_code = None ):
10
+
11
+ payload = {'redirect_uri' :'http://localhost' , 'client_id' : SPOTIFY_CLIENT_ID , 'client_secret' : SPOTIFY_CLIENT_SECRET }
12
+
10
13
if auth_code is None :
11
14
#CHANGE SCOPES HERE
12
15
scopes = ["user-modify-playback-state" , "user-read-playback-state" , "playlist-read-private" , "playlist-read-collaborative" , "streaming" ]
13
16
14
- print ("Go to the following url, and after clicking ok, copy and paste the link you are redirected to from your browser starting with 'localhost'\n " )
17
+ print ("\n \n Go to the following url, and after clicking ok, copy and paste the link you are redirected to from your browser starting with 'localhost'\n " )
15
18
print ("https://accounts.spotify.com/authorize/?client_id=" + SPOTIFY_CLIENT_ID + "&response_type=code&redirect_uri=http://localhost&scope=" + "%20" .join (scopes ))
16
19
17
20
url = input ("\n Paste localhost url: " )
18
21
parsed_url = urlparse (url )
19
- auth_code = parsed_url .query .split ('=' )[1 ]
22
+ payload ['grant_type' ] = 'authorization_code'
23
+ payload ['code' ] = parsed_url .query .split ('=' )[1 ]
24
+ auth_code = {}
25
+
26
+ else :
27
+ payload ['grant_type' ] = 'refresh_token'
28
+ payload ['refresh_token' ] = auth_code ["refresh_token" ]
29
+
20
30
21
- payload = {'grant_type' : 'authorization_code' , 'code' : auth_code , 'redirect_uri' :'http://localhost' , 'client_id' : SPOTIFY_CLIENT_ID , 'client_secret' : SPOTIFY_CLIENT_SECRET }
22
31
result = requests .post ("https://accounts.spotify.com/api/token" , data = payload )
23
32
24
33
response_json = result .json ()
25
34
cur_seconds = time .time ()
26
- response_json ['expires_at' ] = cur_seconds + response_json ["expires_in" ] - 60
35
+ auth_code ['expires_at' ] = cur_seconds + response_json ["expires_in" ] - 60
36
+ auth_code ['access_token' ] = response_json ["access_token" ]
37
+
38
+ if "refresh_token" in response_json :
39
+ auth_code ['refresh_token' ] = response_json ["refresh_token" ]
40
+
27
41
with open ('auth.json' , 'w' ) as outfile :
28
- json .dump (response_json , outfile )
29
- return response_json
42
+ json .dump (auth_code , outfile )
43
+ return auth_code
30
44
31
45
32
46
def get_valid_auth_header ():
33
47
with open ('auth.json' , 'r' ) as infile :
34
48
auth = json .load (infile )
35
49
if time .time () > auth ["expires_at" ]:
36
- auth = authorize (auth ["refresh_token" ])
37
- cur_seconds = time .time ()
38
- auth ['expires_at' ] = cur_seconds + auth ["expires_in" ] - 60
39
- with open ('auth.json' , 'w' ) as outfile :
40
- json .dump (auth , outfile )
50
+ auth = authorize (auth )
41
51
return {"Authorization" : "Bearer " + auth ["access_token" ]}
42
52
43
53
@@ -116,7 +126,4 @@ def get_playlists(limit=None, offset=None):
116
126
# set_device_volume(30)
117
127
# pause_active_music()
118
128
119
-
120
- #play_on_device(device_id='636c557009757a9ef284656942ccc42fa5ec466f', context_uri='spotify:user:12158782677:playlist:3yarNTAIbBCLS93KZqkjDK')
121
-
122
- print ('end' )
129
+ #print('')
0 commit comments