我训练我自己的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()
为什么文档提供的范围不起作用? 谢谢:)
答案 0 :(得分:0)
您可能使用了错误的密码。根据您已链接的doc,应从“权限”部分中获取它:
- 在开发人员控制台中打开技能列表,然后单击“编辑”以获取您的技能。 2.在“构建”选项卡上,单击左下方的权限部分。 3.滚动到“权限”页面底部的Alexa技能消息传递部分。 4.复制客户端ID和客户端密钥值供以后使用。
根据您共享的屏幕-您正在使用帐户关联标签中的值。
您的代码可以与“权限”标签中的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状态。