From 89acfa98efbacabe5bf290d12f02c996ae1a700e Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 27 Feb 2019 21:42:21 +0000 Subject: [PATCH] initial commit with token refresh --- .gitignore | 2 ++ main.py | 6 ++++++ spotframework/__init__.py | 0 spotframework/net/__init__.py | 0 spotframework/net/auth.py | 23 +++++++++++++++++++++++ 5 files changed, 31 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 spotframework/__init__.py create mode 100644 spotframework/net/__init__.py create mode 100644 spotframework/net/auth.py 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']