Skip to content

Commit aa66181

Browse files
committed
fix refresh_token
1 parent b66d107 commit aa66181

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

spotify.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,47 @@
77

88

99
def authorize(auth_code=None):
10+
11+
payload = {'redirect_uri':'http://localhost', 'client_id': SPOTIFY_CLIENT_ID, 'client_secret': SPOTIFY_CLIENT_SECRET}
12+
1013
if auth_code is None:
1114
#CHANGE SCOPES HERE
1215
scopes = ["user-modify-playback-state", "user-read-playback-state", "playlist-read-private", "playlist-read-collaborative", "streaming"]
1316

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\nGo to the following url, and after clicking ok, copy and paste the link you are redirected to from your browser starting with 'localhost'\n")
1518
print("https://accounts.spotify.com/authorize/?client_id=" + SPOTIFY_CLIENT_ID + "&response_type=code&redirect_uri=http://localhost&scope=" + "%20".join(scopes))
1619

1720
url = input("\nPaste localhost url: ")
1821
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+
2030

21-
payload = {'grant_type': 'authorization_code', 'code': auth_code, 'redirect_uri':'http://localhost', 'client_id': SPOTIFY_CLIENT_ID, 'client_secret': SPOTIFY_CLIENT_SECRET}
2231
result = requests.post("https://accounts.spotify.com/api/token", data=payload)
2332

2433
response_json = result.json()
2534
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+
2741
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
3044

3145

3246
def get_valid_auth_header():
3347
with open('auth.json', 'r') as infile:
3448
auth = json.load(infile)
3549
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)
4151
return {"Authorization": "Bearer " + auth["access_token"]}
4252

4353

@@ -116,7 +126,4 @@ def get_playlists(limit=None, offset=None):
116126
# set_device_volume(30)
117127
# pause_active_music()
118128

119-
120-
#play_on_device(device_id='636c557009757a9ef284656942ccc42fa5ec466f', context_uri='spotify:user:12158782677:playlist:3yarNTAIbBCLS93KZqkjDK')
121-
122-
print('end')
129+
#print('')

0 commit comments

Comments
 (0)