|
@@ -38,6 +38,8 @@ class AppToAppView(View):
|
|
|
|
|
|
def validation(self, request_dict, operation, request):
|
|
def validation(self, request_dict, operation, request):
|
|
response = ResponseObject()
|
|
response = ResponseObject()
|
|
|
|
+ if operation == 'updateToken': # 更新token
|
|
|
|
+ return self.update_token(request_dict, response)
|
|
token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
if token.code != 0:
|
|
if token.code != 0:
|
|
return response.json(token.code)
|
|
return response.json(token.code)
|
|
@@ -57,6 +59,30 @@ class AppToAppView(View):
|
|
else:
|
|
else:
|
|
return response.json(414)
|
|
return response.json(414)
|
|
|
|
|
|
|
|
+ @staticmethod
|
|
|
|
+ def update_token(request_dict, response):
|
|
|
|
+ user_id = request_dict.get('user_id', None)
|
|
|
|
+ amazon_access_token = request_dict.get('access_token', None)
|
|
|
|
+ amazon_refresh_token = request_dict.get('refresh_token', None)
|
|
|
|
+ if not all([user_id, amazon_access_token, amazon_refresh_token]):
|
|
|
|
+ return response.json(444)
|
|
|
|
+
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ try:
|
|
|
|
+ # 保存令牌数据
|
|
|
|
+ alexa_oauth_qs = AlexaOauth.objects.filter(user_id=user_id)
|
|
|
|
+ if alexa_oauth_qs.exists():
|
|
|
|
+ alexa_oauth_qs.update(amazon_access_token=amazon_access_token,
|
|
|
|
+ amazon_refresh_token=amazon_refresh_token,
|
|
|
|
+ update_time=now_time)
|
|
|
|
+ else:
|
|
|
|
+ AlexaOauth.objects.create(user_id=user_id, amazon_access_token=amazon_access_token,
|
|
|
|
+ amazon_refresh_token=amazon_refresh_token, create_time=now_time,
|
|
|
|
+ update_time=now_time)
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
@staticmethod
|
|
@staticmethod
|
|
def get_alexa_app_url_and_lwa_fallback_url(response):
|
|
def get_alexa_app_url_and_lwa_fallback_url(response):
|
|
skill_stage = LOOCAM_SKILL_STAGE
|
|
skill_stage = LOOCAM_SKILL_STAGE
|
|
@@ -163,14 +189,6 @@ class AppToAppView(View):
|
|
alexa_oauth_qs.update(link_status=0)
|
|
alexa_oauth_qs.update(link_status=0)
|
|
else:
|
|
else:
|
|
res_data = eval(r.content)
|
|
res_data = eval(r.content)
|
|
- # 连接状态为2,token失效,已连接
|
|
|
|
- elif link_status == 2:
|
|
|
|
- res_data = {
|
|
|
|
- 'accountLink': {
|
|
|
|
- 'status': 'LINKED'
|
|
|
|
- },
|
|
|
|
- 'status': 'ENABLED'
|
|
|
|
- }
|
|
|
|
return response.json(0, res_data)
|
|
return response.json(0, res_data)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
@@ -203,12 +221,9 @@ class AppToAppView(View):
|
|
if request_method not in ['get', 'delete']:
|
|
if request_method not in ['get', 'delete']:
|
|
return
|
|
return
|
|
alexa_oauth_qs = AlexaOauth.objects.filter(user_id=user_id).\
|
|
alexa_oauth_qs = AlexaOauth.objects.filter(user_id=user_id).\
|
|
- values('alexa_api_endpoint', 'amazon_refresh_token', 'link_status')
|
|
|
|
|
|
+ values('alexa_api_endpoint', 'amazon_refresh_token')
|
|
if not alexa_oauth_qs:
|
|
if not alexa_oauth_qs:
|
|
return
|
|
return
|
|
- # 已连接,但token失效
|
|
|
|
- if alexa_oauth_qs[0]['link_status'] == 2:
|
|
|
|
- return
|
|
|
|
now_time = int(time.time())
|
|
now_time = int(time.time())
|
|
# 使用刷新令牌获取新的访问令牌
|
|
# 使用刷新令牌获取新的访问令牌
|
|
# https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#using-refresh-tokens
|
|
# https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#using-refresh-tokens
|
|
@@ -268,7 +283,7 @@ class AppToAppView(View):
|
|
# 获取令牌
|
|
# 获取令牌
|
|
refresh_token = res['res']['refresh_token']
|
|
refresh_token = res['res']['refresh_token']
|
|
redirect_uri += '&token={}&token_type=Bearer&expiration_time=3600'.format(refresh_token)
|
|
redirect_uri += '&token={}&token_type=Bearer&expiration_time=3600'.format(refresh_token)
|
|
- AlexaOauth.objects.filter(user_id=user_id).update(link_status=2)
|
|
|
|
|
|
+ AlexaOauth.objects.filter(user_id=user_id).update(link_status=1)
|
|
else:
|
|
else:
|
|
AlexaOauth.objects.filter(user_id=user_id).update(link_status=0)
|
|
AlexaOauth.objects.filter(user_id=user_id).update(link_status=0)
|
|
redirect_uri += '&error=access_denied&error_description=The%20user%20denied%20the%20request.%20'
|
|
redirect_uri += '&error=access_denied&error_description=The%20user%20denied%20the%20request.%20'
|