-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathauthorization.py
75 lines (60 loc) · 2.17 KB
/
authorization.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
74
75
"""
Based on code written by sammachin.
See https://github.com/sammachin/AlexaCHIP for the original code
"""
import cherrypy
import os
from cherrypy.process import servers
import requests
import json
import threading
import urllib.parse
import helper
class Start(object):
def __init__(self, config):
self.config = config
def index(self):
sd = json.dumps({
"alexa:all": {
"productID": self.config['ProductID'],
"productInstanceAttributes": {
"deviceSerialNumber": "123456"
}
}
})
callback = cherrypy.url() + "code"
payload = {
"client_id": self.config['Client_ID'],
"scope": "alexa:all",
"scope_data": sd,
"response_type": "code",
"redirect_uri": callback
}
req = requests.Request('GET', "https://www.amazon.com/ap/oa", params=payload)
p = req.prepare()
raise cherrypy.HTTPRedirect(p.url)
def code(self, var=None, **params):
code = urllib.parse.quote(cherrypy.request.params['code'])
callback = cherrypy.url()
payload = {
"client_id": self.config['Client_ID'],
"client_secret": self.config['Client_Secret'],
"code": code,
"grant_type": "authorization_code",
"redirect_uri": callback
}
url = "https://api.amazon.com/auth/o2/token"
r = requests.post(url, data=payload)
resp = r.json()
self.config['refresh_token'] = resp['refresh_token']
helper.write_dict('config.dict',self.config)
threading.Timer(1, lambda: cherrypy.engine.exit()).start()
return "Authentication successful! Please return to the program."
index.exposed = True
code.exposed = True
def get_authorization():
# Load configuration dictionary
config = helper.read_dict('config.dict')
cherrypy.config.update({'server.socket_host': '0.0.0.0', })
cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '5000')), })
cherrypy.quickstart(Start(config))