|
@@ -1,9 +1,12 @@
|
|
|
# @Author : Rocky
|
|
|
# @File : AlexaController.py
|
|
|
# @Time : 2023/12/25 10:46
|
|
|
+import time
|
|
|
+
|
|
|
import requests
|
|
|
from django.views import View
|
|
|
|
|
|
+from Model.models import AlexaOauth
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Ansjer.config import CONFIG_INFO, CONFIG_EUR
|
|
@@ -30,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 == 'disableSkillAndUnlinkAccount': # 取消连接skill和账号
|
|
|
+ return self.disable_skill_and_unlink_account(user_id, response)
|
|
|
elif operation == 'getSkillPageURL': # 获取skill页面URL(取消链接)
|
|
|
return self.get_skill_page_url(response)
|
|
|
else:
|
|
@@ -59,6 +64,7 @@ class AppToAppView(View):
|
|
|
if not amazon_authorization_code:
|
|
|
return response.json(444)
|
|
|
|
|
|
+ now_time = int(time.time())
|
|
|
# 获取亚马逊访问令牌,https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#access-token-request
|
|
|
amazon_base_uri = 'https://api.amazon.com'
|
|
|
url = amazon_base_uri + '/auth/o2/token'
|
|
@@ -76,6 +82,17 @@ class AppToAppView(View):
|
|
|
res_data = eval(r.content)
|
|
|
assert res_data.get('access_token')
|
|
|
amazon_access_token = res_data['access_token']
|
|
|
+ amazon_refresh_token = res_data['refresh_token']
|
|
|
+ # 保存令牌数据
|
|
|
+ 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)
|
|
|
|
|
|
# 获取用户授权码
|
|
|
url = redirect_uri + '/appToApp/oa2/getAuthCode'
|
|
@@ -102,7 +119,7 @@ class AppToAppView(View):
|
|
|
# 请求连接skill,https://developer.amazon.com/en-US/docs/alexa/smapi/skill-enablement.html
|
|
|
headers = {
|
|
|
'Content-Type': 'application/json',
|
|
|
- 'Authorization': "Bearer {}".format(amazon_access_token)
|
|
|
+ 'Authorization': 'Bearer {}'.format(amazon_access_token)
|
|
|
}
|
|
|
alexa_api_endpoint_list = ['api.amazonalexa.com', 'api.eu.amazonalexa.com', 'api.fe.amazonalexa.com']
|
|
|
skill_id = 'amzn1.ask.skill.ff5a5074-7ec7-442b-979b-cb57095f7a94'
|
|
@@ -110,12 +127,34 @@ class AppToAppView(View):
|
|
|
url = 'https://{}/v1/users/~current/skills/{}/enablement'.format(alexa_api_endpoint, skill_id)
|
|
|
r = requests.post(headers=headers, url=url, json=data, timeout=30)
|
|
|
if r.status_code == 201:
|
|
|
+ AlexaOauth.objects.filter(user_id=user_id).update(alexa_api_endpoint=alexa_api_endpoint)
|
|
|
res_data = eval(r.content)
|
|
|
return response.json(0, res_data)
|
|
|
return response.json(0)
|
|
|
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_access_token')
|
|
|
+ if not alexa_oauth_qs:
|
|
|
+ return response.json(173)
|
|
|
+ try:
|
|
|
+ alexa_api_endpoint = alexa_oauth_qs[0]['alexa_api_endpoint']
|
|
|
+ amazon_access_token = alexa_oauth_qs[0]['amazon_access_token']
|
|
|
+
|
|
|
+ headers = {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': 'Bearer {}'.format(amazon_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)
|
|
|
+ assert r.status_code == 200
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
@staticmethod
|
|
|
def get_skill_page_url(response):
|
|
|
skill_asin = 'B0C94Q7H1L'
|