Alexa 技能帐户链接 Google API 凭据,刷新令牌时出现问题

Alexa skill account linking with Google APIs credentials, problem refreshing token

我在 Alexa 帐户关联授权方面遇到了一些问题。

这些是我遵循的步骤:

  1. 我从 Google 云控制台
  2. 获得了凭据(客户端 ID、客户端密码...)
  3. 在 Alexa Developer Console 上设置,使用 'Auth Code Grant' 作为授权授予类型
  4. 在我的 Alexa 应用程序上激活了技能并使用我的 Google 帐户成功登录
  5. 现在我在请求中获得了访问令牌,在 handler_input.request_envelope.context.system.user.access_token

问题是访问令牌在一小时后过期,Alexa 不管理令牌的刷新

我应该怎么做才能避免在一小时后每次都要求我的用户登录?我应该使用 Implicit grant 作为授权类型吗?我应该以某种方式获得刷新令牌吗?

附加信息:这是一种使用Python3

连接到 AWS Lambda 的自定义技能

你读过 https://developer.amazon.com/docs/account-linking/configure-authorization-code-grant.html 了吗?

我的猜测是缺少刷新令牌,因为您已经对应用程序进行了授权。 RT 仅发布一次。尝试进入 https://myaccount.google.com/permissions?utm_source=google-account&utm_medium=web 撤销权限,然后重试。

虽然@pinoyyid 的回答是正确的,但它没有提供解决方案,所以我发布一个以供将来参考。

问题确实是亚马逊服务器没有收到来自 Google 的刷新令牌,因此无法在一小时过期后刷新访问令牌。

根据 this link 和其他亚马逊论坛帖子,我找到了一个可行的解决方案。

Amazon Alexa 开发者控制台'Account Linking'配置:

  • 授权授予类型:授权码授予
  • 授权 URI:https://accounts.google.com/o/oauth2/v2/auth?access_type=offline(即使来自 google 凭证的那个不是 v2,也不应该有什么区别) 访问类型非常重要 因为,正如文档所述:

    Set the [access_type] value to offline if your application needs to refresh access tokens when the user is not present at the browser. [...] This value instructs the Google authorization server to return a refresh token and an access token the first time that your application exchanges an authorization code for tokens.

  • 访问令牌 URI:https://accounts.google.com/o/oauth2/token
  • 客户端 ID 和密码:在 Google 云平台上下载
  • 客户端身份验证方案:HTTP 基本
  • 域列表:google.comgoogleapis.com
  • 默认访问令牌过期时间:留空

现在,执行此操作并保存配置后,请注意您可能不会注意到更改,因为来自 here:

When Alexa uses the refresh token to retrieve a new access token for an existing user, Alexa uses the access token URI that was configured at the time the user linked their account. Therefore, if you change the access token URI later, users who linked their accounts before continue to use the old URI for retrieving updated tokens. The users must unlink and re-link accounts to switch to the new access token URI.

所以,为了完成程序:

  1. 停用你的技能
  2. 转到 Google third party applications that have access to your data 并删除关联的 Google 项目
  3. 重新激活您的技能并再次登录(如果操作正确,它应该再次询问您在 Alexa 开发者控制台中指定的范围的权限
  4. 完成!一小时后你应该重试,它应该有一个更新的访问令牌

附加信息

我发现很多人建议检索刷新令牌,我不认为这是可能的,因为即使 Google 发送它,也是亚马逊存储它并使用它来刷新访问令牌。

编辑: 这适用于开发和测试,但我发现 here 出于 public 目的,您必须拥有将用户重定向到的登录页面。对我来说,只需要创建一个简单的 HTML 页面托管在 public S3 存储桶中,它将请求重定向到我之前写的授权 URI,而访问令牌 URI 必须保持 Google一个。