我正在训练自己的Alexa技能,以便从Python发送消息来打开/关闭设备。我正在阅读官方文档(https://developer.amazon.com/it-IT/docs/alexa/smapi/skill-messaging-api-reference.html),但我无法检索令牌,因为我有无效的作用域。我将附加配置屏幕Account linking configuration代码是
import requests
SKILL_ID = 'amzn1.ask.skill.xxxxx'
SKILL_CLIENT_ID = 'amzn1.application-oa2-client.xxxxx'
SKILL_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
API_TOKEN_URL = 'https://api.amazon.com/auth/O2/token'
def richiediToken():
scope = "alexa:skill_messaging"
payload = "grant_type=client_credentials&scope=" + scope + "&client_id=" + \
SKILL_CLIENT_ID + "&client_secret=" + SKILL_CLIENT_SECRET
headers = {'content-type': 'application/x-www-form-urlencoded'}
print("Header: ", headers)
print("Body: ", payload)
richiestaToken = requests.post(
API_TOKEN_URL, data=payload, headers=headers)
print("Risposta:")
print(richiestaToken.json())
richiediToken()为什么文档提供的作用域不起作用?谢谢您:)
发布于 2020-06-19 16:05:19
你可能使用了一个错误的秘密。根据您链接的doc,您应该从权限部分获取它:
根据您共享的屏幕,您正在使用Account Linking选项卡中的值。
您的代码可以正确使用权限选项卡中的clientId/secret:
λ docker run so-python
Header: {'content-type': 'application/x-www-form-urlencoded'}
Body: grant_type=client_credentials&scope=alexa:skill_messaging&client_id=amzn1.application-oa2-client.(...)&client_secret=(...)
Risposta:
{'access_token': 'Atc|MQEBI(...)', 'scope': 'alexa:skill_messaging', 'token_type': 'bearer', 'expires_in': 3600}稍后发送消息:
token = richiestaToken.json()
authorization = "Bearer " + list(token.values())[0] + ""
print(authorization)
messagePayload= {"data": {"sampleMessage": "Sample Message"}, "expiresAfterSeconds": 60}
messageHeaders = {'Authorization': authorization, 'content-type': 'application/json'}
messageResponse = requests.post(
"https://api.amazonalexa.com/v1/skillmessages/users/amzn1.ask.account.AG4SB7...",
json=messagePayload,
headers=messageHeaders
)
print(messageResponse)它应该返回202 HTTP状态。
https://stackoverflow.com/questions/62398852
复制相似问题