尽管有效的LWA授权令牌

时间:2017-11-20 10:06:48

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

我目前正在努力将亚马逊Alexa与我们现有的系统集成。我必须做的TL; DR版本是我应该能够通过Alexa技能管理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"
}

考虑到auth令牌仅在十分钟(可能十五分钟)之前生成,并且它仍然在其保质期内(大约一小时),这是非常令人惊讶的。

在另一个帖子here中,我读到亚马逊正在研究这个问题(即2017年10月21日)。与此同时,如果你已经制定了另一种方法,你能描述一下并帮助一个人吗?

1 个答案:

答案 0 :(得分:0)

从Node命中SMAPI的最简单方法是使用SMAPI Node.js SDK,并提供here文档。

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

  1. 设置LWA安全配置文件。
  2. 使用ASK CLI通过ask util generate-lwa-tokens --client-id <Client ID> --client-confirmation <Client Secret>将LWA客户端ID和客户端密钥交换为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