|
@@ -33,6 +33,8 @@ class AppToAppView(View):
|
|
|
return self.get_alexa_app_url_and_lwa_fallback_url(response)
|
|
|
elif operation == 'accountLinkWithAmazonAuthorizationCode': # 通过亚马逊授权码连接账号
|
|
|
return self.account_link_with_amazon_authorization_code(user_id, request_dict, response)
|
|
|
+ elif operation == 'getAccountLinkingAndSkillStatus': # 获取账号连接和skill状态
|
|
|
+ return self.get_account_linking_and_skill_status(user_id, response)
|
|
|
elif operation == 'disableSkillAndUnlinkAccount': # 取消连接skill和账号
|
|
|
return self.disable_skill_and_unlink_account(user_id, response)
|
|
|
elif operation == 'getSkillPageURL': # 获取skill页面URL(取消链接)
|
|
@@ -137,44 +139,25 @@ class AppToAppView(View):
|
|
|
except Exception as e:
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
- @staticmethod
|
|
|
- def disable_skill_and_unlink_account(user_id, response):
|
|
|
- alexa_oauth_qs = AlexaOauth.objects.filter(user_id=user_id).values('alexa_api_endpoint', 'amazon_refresh_token')
|
|
|
- if not alexa_oauth_qs:
|
|
|
- return response.json(173)
|
|
|
- now_time = int(time.time())
|
|
|
+ @classmethod
|
|
|
+ def get_account_linking_and_skill_status(cls, user_id, response):
|
|
|
+ request_method = 'get'
|
|
|
try:
|
|
|
- # 使用刷新令牌获取新的访问令牌
|
|
|
- # https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#using-refresh-tokens
|
|
|
- alexa_api_endpoint = alexa_oauth_qs[0]['alexa_api_endpoint']
|
|
|
- amazon_refresh_token = alexa_oauth_qs[0]['amazon_refresh_token']
|
|
|
-
|
|
|
- amazon_base_uri = 'https://api.amazon.com'
|
|
|
- url = amazon_base_uri + '/auth/o2/token'
|
|
|
-
|
|
|
- data = {
|
|
|
- 'grant_type': 'refresh_token',
|
|
|
- 'refresh_token': amazon_refresh_token,
|
|
|
- 'client_id': 'amzn1.application-oa2-client.98a01914518743e481d51115144dafb0',
|
|
|
- 'client_secret': '43353cac67670aefd64a5f95309754ddd6bcfe8a087cc3cad1348b626f64b132'
|
|
|
- }
|
|
|
- r = requests.post(url=url, data=data, timeout=10)
|
|
|
- assert r.status_code == 200
|
|
|
+ r = cls.refresh_access_token(user_id, request_method)
|
|
|
+ if r is None:
|
|
|
+ return response.json(173)
|
|
|
res_data = eval(r.content)
|
|
|
- assert res_data.get('access_token')
|
|
|
- assert res_data.get('refresh_token')
|
|
|
- new_access_token = res_data['access_token']
|
|
|
- new_refresh_token = res_data['refresh_token']
|
|
|
- alexa_oauth_qs.update(amazon_access_token=new_access_token, amazon_refresh_token=new_refresh_token,
|
|
|
- update_time=now_time)
|
|
|
+ return response.json(0, res_data)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
- headers = {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- 'Authorization': 'Bearer {}'.format(new_access_token)
|
|
|
- }
|
|
|
- skill_id = 'amzn1.ask.skill.ff5a5074-7ec7-442b-979b-cb57095f7a94'
|
|
|
- url = 'https://{}/v1/users/~current/skills/{}/enablement'.format(alexa_api_endpoint, skill_id)
|
|
|
- r = requests.delete(headers=headers, url=url, timeout=30)
|
|
|
+ @classmethod
|
|
|
+ def disable_skill_and_unlink_account(cls, user_id, response):
|
|
|
+ request_method = 'delete'
|
|
|
+ try:
|
|
|
+ r = cls.refresh_access_token(user_id, request_method)
|
|
|
+ if r is None:
|
|
|
+ return response.json(173)
|
|
|
# 2xx响应状态码为成功
|
|
|
assert str(r.status_code)[:1] == '2'
|
|
|
return response.json(0)
|
|
@@ -191,3 +174,47 @@ class AppToAppView(View):
|
|
|
'lwa_page_url': lwa_page_url
|
|
|
}
|
|
|
return response.json(0, res)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def refresh_access_token(user_id, request_method):
|
|
|
+ if request_method not in ['get', 'delete']:
|
|
|
+ return None
|
|
|
+ alexa_oauth_qs = AlexaOauth.objects.filter(user_id=user_id).values('alexa_api_endpoint', 'amazon_refresh_token')
|
|
|
+ if not alexa_oauth_qs:
|
|
|
+ return None
|
|
|
+ now_time = int(time.time())
|
|
|
+ # 使用刷新令牌获取新的访问令牌
|
|
|
+ # https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#using-refresh-tokens
|
|
|
+ alexa_api_endpoint = alexa_oauth_qs[0]['alexa_api_endpoint']
|
|
|
+ amazon_refresh_token = alexa_oauth_qs[0]['amazon_refresh_token']
|
|
|
+
|
|
|
+ amazon_base_uri = 'https://api.amazon.com'
|
|
|
+ url = amazon_base_uri + '/auth/o2/token'
|
|
|
+
|
|
|
+ data = {
|
|
|
+ 'grant_type': 'refresh_token',
|
|
|
+ 'refresh_token': amazon_refresh_token,
|
|
|
+ 'client_id': 'amzn1.application-oa2-client.98a01914518743e481d51115144dafb0',
|
|
|
+ 'client_secret': '43353cac67670aefd64a5f95309754ddd6bcfe8a087cc3cad1348b626f64b132'
|
|
|
+ }
|
|
|
+ r = requests.post(url=url, data=data, timeout=10)
|
|
|
+ assert r.status_code == 200
|
|
|
+ res_data = eval(r.content)
|
|
|
+ assert res_data.get('access_token')
|
|
|
+ assert res_data.get('refresh_token')
|
|
|
+ new_access_token = res_data['access_token']
|
|
|
+ new_refresh_token = res_data['refresh_token']
|
|
|
+ alexa_oauth_qs.update(amazon_access_token=new_access_token, amazon_refresh_token=new_refresh_token,
|
|
|
+ update_time=now_time)
|
|
|
+
|
|
|
+ headers = {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': 'Bearer {}'.format(new_access_token)
|
|
|
+ }
|
|
|
+ skill_id = 'amzn1.ask.skill.ff5a5074-7ec7-442b-979b-cb57095f7a94'
|
|
|
+ url = 'https://{}/v1/users/~current/skills/{}/enablement'.format(alexa_api_endpoint, skill_id)
|
|
|
+ if request_method == 'get':
|
|
|
+ r = requests.get(headers=headers, url=url, timeout=30)
|
|
|
+ elif request_method == 'delete':
|
|
|
+ r = requests.delete(headers=headers, url=url, timeout=30)
|
|
|
+ return r
|