首先,我想提一下,此类问题之前已在以下主题中讨论过: How to authenticate user with just a Google account on Actions on Google?
但我根本找不到任何与将 Google Assistant 应用与我自己的网络服务器链接的方式相关的信息。
问题:
获取https://oauth-redirect.googleusercontent.com/r/my-google-assistant-app#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE始终返回“要链接的帐户字段”错误
复制方法:
- 我在 Google Console 上的 Actions 中创建了一个新项目
- 我使用 Dialogflow 构建了一个应用程序
- 我创建了一个 Heroku Web 服务器作为我的 webhook(例如:webhook.herokuapp.com)
所以一切都很好。我已成功将我的 Google Assistant 应用与我的 webhook 连接起来。
但后来我决定向另一台 Heroku Web 服务器(例如:webserver.herokuapp.com)发送请求,该服务器需要用户登录。所以我决定实现帐户链接我的 Google Assistant 应用与 网络服务器.herokuapp.com
- 我在 网络服务器.herokuapp.com 上实现了 Google-OAuth2 授权。另外我想提一下,我使用了 Google Assistant 应用的客户端 ID 和客户端 key
因此,Google 授权在我的网络服务器.herokuapp.com 上运行良好
所以,就是这样!
结语
我打开 Google Assistant,告诉它“与我的测试应用程序对话”,然后它会向我打招呼并让我链接帐户。我说是”。 然后我看到 Google Assistant 如何请求我在 webserver.herokuapp.com 上的登录 URL。然后我输入我的 Google 电子邮件和密码。结果我登录了我的网络服务器!然后我的网络服务器重定向到:
我收到了我在帖子顶部描述的错误。
如有任何帮助,我们将不胜感激
更新参数信息
These are my constants:
Google Assistant Project_ID = nodejs-sdk-local-test
Google Assistant Client_ID = 1067979601708-ldr3ga115es3fdo823slfnj46gjoes66.apps.googleusercontent.com
state = manually_set_state_value
流程是我打开 Google Assistant 并说“Ok Google,与我的测试应用程序交谈”,它回答我的请求,然后我说“执行一些需要授权的操作”。 Google Assistant 应用程序让我链接一个帐户,我说"is"。
这是 Google Assistant 应用调用我的
https://webserver.herokuapp.com/google-oauth2
的地方端点。此端点的目的是将我重定向到 Google 登录页面。现在我被重定向到 Google 登录页面(一切似乎都是正确的。
client_id
和state
参数与常量类似)。这是我被重定向到的 URL 示例:https://accounts.google.com/o/oauth2/auth?client_id=1067979601708-ldr3ga115es3fdo823slfnj46gjoes66.apps.googleusercontent.com&redirect_uri=http://webserver.herokuapp.com/complete/google-oauth2/&state=manually_set_state_value&response_type=code&scope=openid+email+profile
在 Google 登录页面,我输入了我的凭据,然后我被重定向到
redirect_uri=http://webserver.herokuapp.com/complete/google-oauth2
端点。此端点的目的是在webserver.herokuapp.com
注册或登录我。然后将我重定向到另一个 Google URL。 Google URL 示例:https://oauth-redirect.googleusercontent.com/r/nodejs-sdk-local-test#access_token=<token_omitted>&token_type=bearer&state=manually_set_state_value
看来一切都是正确的。我登录了我的
webserver.herokuapp.com
,获得了访问 token 。另外project_id
和state
最后一个 Google URL 中的参数与常量类似。无论如何,这个 Google URL 返回了一个错误“帐户无法链接”!
最佳答案
最常见的原因是,您没有传回 Google 重定向到您的网站时发送给您的相同的 state
值。确保这是相同的值,并且为了您的安全,请确保您也验证发送给您的其他参数。
当用户重定向到 https://webserver.herokuapp.com/google-oauth2
时,应该有一些参数作为查询的一部分发送。这些参数包括一个state
参数。它应该看起来像这样:
https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token
请注意,虽然 client_id
和 redirect_uri
是您应该期望并测试的值,但 state
值不是 您本来应该设置的一个。谷歌每次都会生成一个不同的。这是您稍后重定向到
状态
https://oauth-redirect.googleusercontent.com/r/nodejs-sdk-local-test#access_token=<token_omitted>&token_type=bearer&state=STATE_STRING
关于google-oauth - 当我尝试在我的网络服务上授权 Google Assistant 时,发生 "Accounts failed to link"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48461569/