lang hace 4 años
padre
commit
44babf3ca1
Se han modificado 1 ficheros con 38 adiciones y 1 borrados
  1. 38 1
      controller/deviceStatus.py

+ 38 - 1
controller/deviceStatus.py

@@ -10,6 +10,8 @@ import logging
 from django.views.generic import TemplateView
 from django.shortcuts import render_to_response
 from django.http import JsonResponse
+import http.client
+from urllib.parse import urlencode
 from object.ResObject import ResObject
 import subprocess
 # from gevent.pool import Pool
@@ -42,7 +44,7 @@ class deviceStatus(TemplateView):
             if operation == 'test':
                 api_uri = 'https://api.amazonalexa.com/v3/events'
                 messageId = str(uuid.uuid4())
-                access_token = 'dEqVJw7g3wR4oXOTD6szvNebyKQEkCba'
+                access_token = 'wR2eWfmTTFRLXq5pxSvaGGAI1tfLCfv5'
                 bearer_access_token = 'Bearer {access_token}'.format(access_token=access_token)
                 headers =  {'content-type': "application/json", 'Authorization': bearer_access_token}
                 payload = {
@@ -99,7 +101,42 @@ class deviceStatus(TemplateView):
                 request_json = response.json()
                 return JsonResponse({'res': request_json})
 
+            elif operation == 'getAccessToken':
+                # lwa_params = {
+                #     "grant_type" : "authorization_code",
+                #     "code": 'NHxKR2mgPmNgCp9yxNCJvJ7fgPbCayNa',
+                #     "client_id": 'amzn1.application-oa2-client.efb07b51dd444f848b6f0598635da3cc',
+                #     "client_secret": 'showmeyousecret.zosi'
+                # }
+                # header = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
+                # response = requests.post('https://api.amazon.com/auth/o2/token', data=lwa_params, headers= header, allow_redirects=True)
+                #
+                #
+                # request_json = response.json()
+                # return JsonResponse({'res': request_json})
+                user_qs = UserModel.objects.filter(userID='158943604783713800138000').values('userID','refresh_token')
+                refresh_token = user_qs[0]['refresh_token']
 
+                payload = {
+                    'grant_type': 'refresh_token',
+                    'refresh_token': refresh_token,
+                    'client_id': 'amzn1.application-oa2-client.efb07b51dd444f848b6f0598635da3cc',
+                    'client_secret': '8a49390ebe362bfee153be87587f5673d0c1d8aeb6bc1ef736fda6c9d5d81c8f',
+                    'redirect_uri': 'http://www.baidu.com'
+                }
+                return self.post_to_api(payload)
+
+
+    def post_to_api(self, payload):
+        connection = http.client.HTTPSConnection("api.amazon.com")
+        headers = {
+            'content-type': "application/x-www-form-urlencoded",
+            'cache-control': "no-cache"
+        }
+
+        response = requests.post('https://api.amazon.com/auth/o2/token', urlencode(payload), headers= headers, allow_redirects=True)
+        request_json = response.json()
+        return JsonResponse({'res': request_json})