Google OAuth 2 和 state 参数值需要在重定向 url 中注册

Posted

技术标签:

【中文标题】Google OAuth 2 和 state 参数值需要在重定向 url 中注册【英文标题】:Google OAuth 2 and state parameter values need to be registered in redirect url 【发布时间】:2013-05-07 05:38:57 【问题描述】:

根据 Google Oauth 2.0 文档的状态参数:

表示收到响应后可能对您的应用程序有用的任何状态。 Google 授权服务器会往返此参数,因此您的应用程序会收到它发送的相同值。可能的用途包括将用户重定向到您站点中的正确资源、nonce 和跨站点请求伪造缓解。

我想使用 state 参数来了解原始 oauth 请求是从哪个子域发起的。但是 redirect_state 参数似乎需要注册为“授权重定向 URI”之一的一部分。如果没有,我得到:

错误:redirect_uri_mismatch 请求中的重定向 URI:http://my_server.com/complete/google-oauth2/?redirect_state=2 与注册的重定向 URI 不匹配

我想要一个不需要在授权的重定向 URI 中注册所有可能的 redirect_state 值的解决方案,因为这不是很容易维护。想法?

【问题讨论】:

在***.com/questions/7722062/… 上查看我的答案,谢谢! 【参考方案1】:

参数的名称是state(而不是redirect_state)!

根据 google 文档的示例 OAuth 请求是 ->

https://accounts.google.com/o/oauth2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&
state=%2Fprofile&
redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Fcode&
response_type=code&
client_id=812741506391.apps.googleusercontent.com&approval_prompt=force

请注意State 参数和redirect_uri 参数。我认为你把两者混为一谈了。

编辑 - 请参阅 Google 提供的 link。对状态参数和构建 Web 请求有很好的解释。

【讨论】:

啊,你是对的。可能应该早点去睡觉!谢谢

以上是关于Google OAuth 2 和 state 参数值需要在重定向 url 中注册的主要内容,如果未能解决你的问题,请参考以下文章

关于OAuth的state参数的作用

通过 OAuth 2 连接到 Google“redirect_uri 的参数值无效:缺少权限:”

PHP:Google plus Oauth 2.0 - 获取OAuth2访问令牌时出错,消息:'invalid_client'

OAuth 2.0“状态”和OpenID“nonce”参数之间的区别?为啥状态不能被重用?

状态如何在 Spring OAuth2 中进行编码或加密?

Firebase |从Google oAuth中检索高清参数