【问题标题】:Alexa SMAPI returns HTTP 4xx despite valid LWA authorization token尽管 LWA 授权令牌有效,Alexa SMAPI 仍返回 HTTP 4xx
【发布时间】:2017-11-20 10:06:47
【问题描述】:

我目前正在努力将 Amazon Alexa 与我们当前的系统集成。我必须做的 TL;DR 版本是我应该能够通过 Alexa Skill Management API 以编程方式创建 Alexa 技能。

虽然这很简单,但我在身份验证阶段遇到了障碍(涉及使用亚马逊登录):

这应该起作用的方式是,您对 SMAPI 端点发出的每个请求都必须在 Authorization HTTP 标头中包含授权令牌。

假设我使用此正文向https://api.amazonalexa.com/v0/skills 发出 POST 请求:

{
"vendorId":"my-vendor-id",
"skillManifest": {
    "publishingInformation": {
        "locales": {
            "en-US": {
                "summary": "This is a sample Alexa skill.",
                "examplePhrases": [
                    "Alexa, open sample skill.",
                    "Alexa, turn on kitchen lights.",
                    "Alexa, blink kitchen lights."
                ],
                "keywords": [
                    "Smart Home",
                    "Lights",
                    "Smart Devices"
                ],
                "name": "Sample custom skill name.",
                "description": "This skill has basic and advanced smart devices control features."
            }
        },
        "isAvailableWorldwide": false,
        "testingInstructions": "1) Say 'Alexa, discover my devices' 2) Say 'Alexa, turn on sample lights'",
        "category": "SMART_HOME",
        "distributionCountries": [
            "US",
            "GB"
        ]
    },
    "apis": {
        "custom": {
            "endpoint": {
                "uri": <some-aws-lambda-endpoint-uri>"
            }
        }
    },
    "manifestVersion": "1.0",
    "privacyAndCompliance": {
        "allowsPurchases": false,
        "locales": {
            "en-US": {
                "termsOfUseUrl": "http://www.termsofuse.sampleskill.com",
                "privacyPolicyUrl": "http://www.myprivacypolicy.sampleskill.com"
            }
        },
        "isExportCompliant": true,
        "isChildDirected": false,
        "usesPersonalInfo": false
    }
  }
}

还有这些头域:

{
"Authorization":"<my-auth-token-that-i-get-from-lwa>"
}

预期的响应应采用以下格式:

{
  "skill_id": "{skill_id}"
}

但是,这是我得到的回应:

{
  "message": "User has not consented to this operation"
}

(尽管有问题的用户已经同意这些操作,这在这个权限请求数组中非常明显:

['profile profile:user_id alexa::ask:skills:readwrite alexa::ask:skills:test alexa::ask:models:readwrite alexa::ask:skills:test alexa::ask:models:read alexa::ask:skills:read']

)

此外,如果我以这种方式将 Bearer 前缀添加到 Authorization 标头:

{
    "Authorization":"Bearer <my-lwa-auth-token>"
}

我收到以下回复:

{
    "message": "Token is invalid/expired"
}

考虑到身份验证令牌仅在十(可能十五)分钟前生成,并且仍在其保质期(大约一小时)内,这非常令人惊讶。

在另一个线程 here 中,我读到亚马逊正在处理这个问题(那是 2017 年 10 月 21 日)。同时,如果您想出了另一种方法,请您描述一下并帮助其他人吗?

【问题讨论】:

  • 也面临同样的问题。有什么结果吗?一件奇怪的事情是,我可以毫无问题地使用相同的令牌发出 GET 请求。但是在尝试发布技能时,我收到相同的错误消息:“消息”:“令牌无效/过期”有什么想法/解决方案吗?
  • 不,@LucaKiedrowski。下周我就放弃了这个练习。结果不值得我花时间。
  • 我解决了这个问题。你用什么语言写这个?我用一个模块提出了这个请求,然后尝试手动完成它并且以某种方式工作。
  • 那是用 JavaScript (Node.JS)
  • 使用 Python(使用请求)遇到同样的问题,@LucaKiedrowski,你使用了什么模块?解决这个问题后发生了什么变化?

标签: alexa-skills-kit alexa-skill login-with-amazon


【解决方案1】:

从 Node 访问 SMAPI 的最简单方法是使用 SMAPI Node.js SDK,提供文档here

为了通过 SMAPI 进行身份验证,您需要执行以下操作:

  1. 设置 LWA 安全配置文件。
  2. 使用 ASK CLI 将您的 LWA 客户端 ID 和客户端密码交换为使用 ask util generate-lwa-tokens --client-id &lt;Client ID&gt; --client-confirmation &lt;Client Secret&gt; 的 LWA 刷新令牌。
  3. 在初始化 SMAPI 节点 SDK 时使用此刷新令牌:
const Alexa = require('ask-smapi-sdk');

// specify the refreshTokenConfig with clientId, clientSecret and refreshToken generated in the previous step
const refreshTokenConfig = {
    clientId,
    clientSecret, 
    refreshToken
}
const smapiClient = new Alexa.StandardSmapiClientBuilder()
    .withRefreshTokenConfig(refreshTokenConfig)
    .client();

然后您就可以通过 SDK 上的函数调用来访问 SMAPI!

这方面的有用资源: https://levelup.gitconnected.com/email-yourself-daily-alexa-skill-metrics-updates-using-lambda-smapi-and-ses-9c16ac97c1f8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2020-04-02
    • 2017-04-01
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多