-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.py
More file actions
executable file
·90 lines (74 loc) · 2.46 KB
/
authenticate.py
File metadata and controls
executable file
·90 lines (74 loc) · 2.46 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: UTF-8 -*-
""" Authenticate the user credentials
"""
from lib.AlexaService import AlexaService
from lib.Config import Config
import cherrypy
import os
import requests
import json
import urllib
class Start(object):
""" The Web object
"""
def index(self):
""" The main page
"""
product_id = Config.get_config(Config.FIELD_PRODUCT_ID)
client_id = Config.get_config(Config.FIELD_CLIENT_ID)
scope_data = json.dumps(
{"alexa:all": {
"productID": product_id,
"productInstanceAttributes": {
"deviceSerialNumber": "001"}
}}
)
callback = cherrypy.url() + "code"
payload = {
"client_id": client_id,
"scope": "alexa:all",
"scope_data": scope_data,
"response_type": "code",
"redirect_uri": callback
}
req = requests.Request(
'GET', AlexaService.AMAZON_BASE_URL, params=payload
)
raise cherrypy.HTTPRedirect(req.prepare().url)
def code(self, var=None, **params):
""" The code page
"""
client_id = Config.get_config(Config.FIELD_CLIENT_ID)
client_secret = Config.get_config(Config.FIELD_CLIENT_SECRET)
code = urllib.quote(cherrypy.request.params['code'])
callback = cherrypy.url()
payload = {
"client_id" : client_id,
"client_secret" : client_secret,
"code" : code,
"grant_type" : "authorization_code",
"redirect_uri" : callback
}
result = requests.post(AlexaService.AMAZON_TOKEN_URL, data=payload)
result = result.json()
# Save the refresh token and reset access token
Config.save_config(
Config.FIELD_REFRESH_TOKEN,
format(result['refresh_token'])
)
Config.save_config(Config.FIELD_ACCESS_TOKEN, "")
html = "<b>Success!</b><br/>"
html += "Refresh token has been added to your credentials file.<br/>"
html += "You may now reboot the Pi or restart the service.<br/>"
html += "Your token: %s" % result['refresh_token']
return html
index.exposed = True
code.exposed = True
if __name__ == "__main__":
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())