|
@@ -65,7 +65,8 @@ class AppToAppView(View):
|
|
return response.json(444)
|
|
return response.json(444)
|
|
|
|
|
|
now_time = int(time.time())
|
|
now_time = int(time.time())
|
|
- # 获取亚马逊访问令牌,https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#access-token-request
|
|
|
|
|
|
+ # 获取亚马逊访问令牌
|
|
|
|
+ # https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#access-token-request
|
|
amazon_base_uri = 'https://api.amazon.com'
|
|
amazon_base_uri = 'https://api.amazon.com'
|
|
url = amazon_base_uri + '/auth/o2/token'
|
|
url = amazon_base_uri + '/auth/o2/token'
|
|
redirect_uri = 'https://smart.loocam2.com'
|
|
redirect_uri = 'https://smart.loocam2.com'
|
|
@@ -81,6 +82,7 @@ class AppToAppView(View):
|
|
assert r.status_code == 200
|
|
assert r.status_code == 200
|
|
res_data = eval(r.content)
|
|
res_data = eval(r.content)
|
|
assert res_data.get('access_token')
|
|
assert res_data.get('access_token')
|
|
|
|
+ assert res_data.get('refresh_token')
|
|
amazon_access_token = res_data['access_token']
|
|
amazon_access_token = res_data['access_token']
|
|
amazon_refresh_token = res_data['refresh_token']
|
|
amazon_refresh_token = res_data['refresh_token']
|
|
# 保存令牌数据
|
|
# 保存令牌数据
|
|
@@ -116,7 +118,8 @@ class AppToAppView(View):
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- # 请求连接skill,https://developer.amazon.com/en-US/docs/alexa/smapi/skill-enablement.html
|
|
|
|
|
|
+ # 请求连接skill
|
|
|
|
+ # https://developer.amazon.com/en-US/docs/alexa/smapi/skill-enablement.html
|
|
headers = {
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer {}'.format(amazon_access_token)
|
|
'Authorization': 'Bearer {}'.format(amazon_access_token)
|
|
@@ -136,16 +139,38 @@ class AppToAppView(View):
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def disable_skill_and_unlink_account(user_id, response):
|
|
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')
|
|
|
|
|
|
+ alexa_oauth_qs = AlexaOauth.objects.filter(user_id=user_id).values('alexa_api_endpoint', 'amazon_refresh_token')
|
|
if not alexa_oauth_qs:
|
|
if not alexa_oauth_qs:
|
|
return response.json(173)
|
|
return response.json(173)
|
|
|
|
+ now_time = int(time.time())
|
|
try:
|
|
try:
|
|
|
|
+ # 使用刷新令牌获取新的访问令牌
|
|
|
|
+ # https://developer.amazon.com/zh/docs/login-with-amazon/authorization-code-grant.html#using-refresh-tokens
|
|
|
|
+ amazon_refresh_token = alexa_oauth_qs[0]['amazon_access_token']
|
|
alexa_api_endpoint = alexa_oauth_qs[0]['alexa_api_endpoint']
|
|
alexa_api_endpoint = alexa_oauth_qs[0]['alexa_api_endpoint']
|
|
- amazon_access_token = alexa_oauth_qs[0]['amazon_access_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 = {
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
- 'Authorization': 'Bearer {}'.format(amazon_access_token)
|
|
|
|
|
|
+ 'Authorization': 'Bearer {}'.format(new_access_token)
|
|
}
|
|
}
|
|
skill_id = 'amzn1.ask.skill.ff5a5074-7ec7-442b-979b-cb57095f7a94'
|
|
skill_id = 'amzn1.ask.skill.ff5a5074-7ec7-442b-979b-cb57095f7a94'
|
|
url = 'https://{}/v1/users/~current/skills/{}/enablement'.format(alexa_api_endpoint, skill_id)
|
|
url = 'https://{}/v1/users/~current/skills/{}/enablement'.format(alexa_api_endpoint, skill_id)
|