-
Notifications
You must be signed in to change notification settings - Fork 15
/
auth_web.py
57 lines (51 loc) · 1.72 KB
/
auth_web.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
#! /usr/bin/env python
from alexa_client.settings import PRODUCT_ID, CLIENT_ID, CLIENT_SECRET, WEB_PORT
import cherrypy
import json
import os
import requests
import urllib
import uuid
class Start(object):
def index(self):
sd = json.dumps({
"alexa:all": {
"productID": PRODUCT_ID,
"productInstanceAttributes": {
"deviceSerialNumber": uuid.getnode()
}
}
})
url = "https://www.amazon.com/ap/oa"
callback = cherrypy.url() + "authresponse"
payload = {
"client_id": CLIENT_ID,
"scope": "alexa:all",
"scope_data": sd,
"response_type": "code",
"redirect_uri": callback
}
req = requests.Request('GET', url, params=payload)
p = req.prepare()
raise cherrypy.HTTPRedirect(p.url)
def authresponse(self, var=None, **params):
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
}
url = "https://api.amazon.com/auth/o2/token"
r = requests.post(url, data=payload)
resp = r.json()
return "Success! Here is your refresh token:<br>{}".format(
resp['refresh_token'])
index.exposed = True
authresponse.exposed = True
cherrypy.config.update({'server.socket_host': 'localhost'})
cherrypy.config.update({'server.socket_port': WEB_PORT})
print('Open http://localhost:3000 to login in amazon alexa service')
cherrypy.quickstart(Start())