|
@@ -6,7 +6,6 @@ from django.views import View
|
|
|
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
-from Ansjer.config import SERVER_DOMAIN_SSL
|
|
|
|
|
|
|
|
|
class AppToAppView(View):
|
|
@@ -25,10 +24,11 @@ class AppToAppView(View):
|
|
|
token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
if token.code != 0:
|
|
|
return response.json(token.code)
|
|
|
+ user_id = token.userID
|
|
|
if operation == 'getAlexaAppURLAndLWAFallbackURL': # 获取Alexa App和LWA fallback URL
|
|
|
return self.get_alexa_app_url_and_lwa_fallback_url(response)
|
|
|
- elif operation == 'exchangesTheAmazonAuthorizationCode': # 根据亚马逊验证码获取亚马逊访问令牌
|
|
|
- return self.exchanges_the_amazon_authorization_code(request_dict, response)
|
|
|
+ elif operation == 'accountLinkWithAmazonAuthorizationCode': # 通过亚马逊授权码连接账号
|
|
|
+ return self.account_link_with_amazon_authorization_code(user_id, request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
@@ -36,12 +36,6 @@ class AppToAppView(View):
|
|
|
def get_alexa_app_url_and_lwa_fallback_url(response):
|
|
|
client_id = 'amzn1.application-oa2-client.98a01914518743e481d51115144dafb0' # Alexa Client Id
|
|
|
skill_stage = 'development' # 开发中: development, 已上线: live
|
|
|
- """
|
|
|
- Alexa Redirect URLs
|
|
|
- https://pitangui.amazon.com/api/skill/link/M1GQ2T9YG53MFV
|
|
|
- https://alexa.amazon.co.jp/api/skill/link/M1GQ2T9YG53MFV
|
|
|
- https://layla.amazon.com/api/skill/link/M1GQ2T9YG53MFV
|
|
|
- """
|
|
|
redirect_uri = 'https://smart.loocam2.com'
|
|
|
alexa_app_url = 'https://alexa.amazon.com/spa/skill-account-linking-consent?' \
|
|
|
'fragment=skill-account-linking-consent&client_id={}&' \
|
|
@@ -57,18 +51,57 @@ class AppToAppView(View):
|
|
|
return response.json(0, res)
|
|
|
|
|
|
@staticmethod
|
|
|
- def exchanges_the_amazon_authorization_code(request_dict, response):
|
|
|
+ def account_link_with_amazon_authorization_code(user_id, request_dict, response):
|
|
|
amazon_authorization_code = request_dict.get('amazon_authorization_code', None)
|
|
|
if not amazon_authorization_code:
|
|
|
return response.json(444)
|
|
|
|
|
|
+ # 获取亚马逊访问令牌
|
|
|
url = 'https://api.amazon.com/auth/o2/token'
|
|
|
+ base_uri = 'https://smart.loocam2.com'
|
|
|
data = {
|
|
|
'grant_type': 'authorization_code',
|
|
|
'code': amazon_authorization_code,
|
|
|
'client_id': 'amzn1.application-oa2-client.98a01914518743e481d51115144dafb0',
|
|
|
'client_secret': '43353cac67670aefd64a5f95309754ddd6bcfe8a087cc3cad1348b626f64b132',
|
|
|
- 'redirect_uri': amazon_authorization_code,
|
|
|
+ 'redirect_uri': base_uri
|
|
|
}
|
|
|
- requests.post(url=url, data=data)
|
|
|
+ r = requests.post(url=url, data=data)
|
|
|
+ if r.status_code != 200:
|
|
|
+ return response.json(10, '{}请求响应状态码错误:{}'.format(url, r.status_code))
|
|
|
+ res_data = eval(r.content)
|
|
|
+ print(res_data)
|
|
|
+ if res_data.get('access_token'):
|
|
|
+ amazon_access_token = res_data['access_token']
|
|
|
+ else:
|
|
|
+ return response.json(10, '{}请求响应缺失令牌:{}'.format(url, res_data))
|
|
|
+
|
|
|
+ # 获取用户授权码
|
|
|
+ url = base_uri + '/appToApp/oa2/getAuthCode'
|
|
|
+ params = {
|
|
|
+ 'user_id': user_id
|
|
|
+ }
|
|
|
+ r = requests.get(url=url, params=params)
|
|
|
+ if r.status_code != 200:
|
|
|
+ return response.json(10, '{}请求响应状态码错误:{}'.format(url, r.status_code))
|
|
|
+ res = eval(r.content)
|
|
|
+ user_authorization_code = res['res']['user_authorization_code']
|
|
|
+ data = {
|
|
|
+ "stage": "development",
|
|
|
+ "accountLinkRequest": {
|
|
|
+ "redirectUri": base_uri,
|
|
|
+ "authCode": user_authorization_code,
|
|
|
+ "type": "AUTH_CODE"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ # 请求连接skill
|
|
|
+ headers = {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': "Bearer {}".format(amazon_access_token)
|
|
|
+ }
|
|
|
+ skill_id = 'amzn1.ask.skill.ff5a5074-7ec7-442b-979b-cb57095f7a94'
|
|
|
+ url = 'https://api.amazonalexa.com/v1/users/~current/skills/{}/enablement'.format(skill_id)
|
|
|
+ get_token_res = requests.post(headers=headers, url=url, json=data)
|
|
|
+ res_data = eval(get_token_res.content)
|
|
|
+ print(res_data)
|
|
|
return response.json(0)
|