commit 89acfa98efbacabe5bf290d12f02c996ae1a700e Author: aj Date: Wed Feb 27 21:42:21 2019 +0000 initial commit with token refresh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..151121e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +env +__pycache__ diff --git a/main.py b/main.py new file mode 100644 index 0000000..7628f0b --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +import spotframework.net.auth as auth + +if __name__ == '__main__': + print('hello world') + auth.refreshToken() + diff --git a/spotframework/__init__.py b/spotframework/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spotframework/net/__init__.py b/spotframework/net/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spotframework/net/auth.py b/spotframework/net/auth.py new file mode 100644 index 0000000..3ca7b91 --- /dev/null +++ b/spotframework/net/auth.py @@ -0,0 +1,23 @@ +import os +import requests +from base64 import b64encode + +client_id = os.environ['SPOTCLIENT'] +client_secret = os.environ['SPOTSECRET'] +user_access_token = os.environ['SPOTACCESS'] +user_refresh_token = os.environ['SPOTREFRESH'] + +#This sets up the https connection +#we need to base 64 encode it +#and then decode it to acsii as python 3 stores it as a byte string +def refreshToken(): + + idsecret = b64encode(bytes(client_id + ':' + client_secret, "utf-8")).decode("ascii") + headers = { 'Authorization' : 'Basic %s' % idsecret } + + data = {"grant_type": "refresh_token", "refresh_token": user_refresh_token} + + req = requests.post('https://accounts.spotify.com/api/token', data = data, headers = headers ) + + if req.status_code == 200: + user_access_token = req.json()['access_token']