This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
73 lines (60 loc) · 2.16 KB
/
__init__.py
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
class storage():
def __init__(self):
self.clientid = ""
self.clientsecret = ""
class twitchlocal():
def __init__(self, storage):
self.storage = storage
self.refresh = False
pass
def gettoken(self):
from requests import post
url = "https://id.twitch.tv/oauth2/token?client_id={clientid}&client_secret={clientsecret}&grant_type=client_credentials".format(clientid=storage.clientid,clientsecret=storage.clientsecret)
rtn = post(url).json()
rtn.update({"client_id": self.storage.clientid})
return rtn
def revoketoken(self, arg):
from requests import post
url = "https://id.twitch.tv/oauth2/revoke?client_id={clientid}&token={token}".format(clientid=arg["client_id"], token=arg["token"])
rtn = post(url)
try:
return rtn.json()
except:
return {"status": 200}
def refreshtoken(self, arg):
if arg["client_id"] != storage.clientid: return {"status": 400, "line": "invalid client id"}
rtn = self.revoketoken(arg)
if rtn["status"] != 200: return rtn
else:
rtn = self.gettoken()
rtn.update({"mode": "refresh"})
return rtn
from flask import app, Flask, render_template, request
from flask_compress import Compress
import json
compress = Compress()
app = Flask(__name__)
storage = storage()
@app.route('/gettoken', methods=['GET'])
def gettocken():
return twitchlocal(storage).gettoken()
@app.route('/revoketoken', methods=['GET'])
def revoketocken():
arg = request.args
if "token" in arg:
pass
else: return {"status": 400, "line":"cannot find token"}
if "client_id" in arg:
pass
else: return {"status": 400, "line":"cannot find clientid"}
return twitchlocal(storage).revoketoken(arg)
@app.route('/refreshtoken', methods=['GET'])
def refreshtoken():
arg = request.args
if "token" in arg:
pass
else: return {"status": 400, "line":"cannot find token"}
if "client_id" in arg:
pass
else: return {"status": 400, "line":"cannot find clientid"}
return twitchlocal(storage).refreshtoken(arg)