-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcredentials.py
94 lines (75 loc) · 2.64 KB
/
credentials.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env python
from __future__ import print_function
import os
import json
import socket
import uuid
import hashlib
import yaml
import cherrypy
import requests
import alsaaudio
try:
from urllib.parse import quote
except ImportError:
from urllib import quote
import alexapi.config
class Start(object):
def index(self):
sd = json.dumps({
"alexa:all": {
"productID": config['alexa']['Device_Type_ID'],
"productInstanceAttributes": {
"deviceSerialNumber": hashlib.sha256(str(uuid.getnode()).encode()).hexdigest()
}
}
})
url = "https://www.amazon.com/ap/oa"
callback = cherrypy.url().replace("http:", "https:") + "code"
payload = {
"client_id": config['alexa']['Client_ID'],
"scope": "alexa:all",
"scope_data": sd,
"response_type": "code",
"redirect_uri": callback
}
req = requests.Request('GET', url, params=payload)
prepared_req = req.prepare()
raise cherrypy.HTTPRedirect(prepared_req.url)
def code(self, var=None, **params): # pylint: disable=unused-argument
code = quote(cherrypy.request.params['code'])
callback = cherrypy.url().replace("http:", "https:")
payload = {
"client_id": config['alexa']['Client_ID'],
"client_secret": config['alexa']['Client_Secret'],
"code": code,
"grant_type": "authorization_code",
"redirect_uri": callback
}
url = "https://api.amazon.com/auth/o2/token"
response = requests.post(url, data=payload)
resp = response.json()
alexapi.config.set_variable(['alexa', 'refresh_token'], resp['refresh_token'])
cherrypy.engine.exit()
return (
"<h2>Success!</h2><h3> Refresh token has been added to your "
"config file, you may now reboot the Pi </h3><br>{}"
).format(resp['refresh_token'])
index.exposed = True
code.exposed = True
cherrypy.config.update({'server.socket_host': '0.0.0.0'})
cherrypy.config.update({'server.socket_port': int(os.environ.get('PORT', '80'))})
cherrypy.config.update({"environment": "embedded"})
credentials = ['Client_ID', 'Client_Secret', 'Device_Type_ID', 'Security_Profile_Description', 'Security_Profile_ID']
for cred in credentials:
alexapi.config.set_variable(['alexa', cred], os.environ.get(cred))
# Configure input_device
input_devices = alsaaudio.pcms(alsaaudio.PCM_CAPTURE)
alexapi.config.set_variable(['sound', 'input_device'], input_devices[len(input_devices) - 1])
with open(alexapi.config.filename, 'r') as stream:
config = yaml.load(stream)
# need to login to Amazon
if not config['alexa']['refresh_token']:
public_url = os.environ.get('RESIN_DEVICE_UUID') + '.resindevice.io'
print("Go to https://{} to begin the auth process".format(public_url))
cherrypy.quickstart(Start())