我正在构建一个基于Restful的API Rails 5、devise_token_auth和Omniauth google Oauth2。
目前通过电子邮件创建基本帐户运行得很好,现在我正在尝试使用 Google 配置身份验证,但在身份验证过程中我遇到了一些无法解决的错误。
下面是我面临的不同错误:
Started GET "/omniauth/google_oauth2/callback?state=xxxxx&code=xxxxx" for 127.0.0.1 at 2018-04-24 20:57:55 +0800
I, [2018-04-24T20:57:55.496909 #14173] INFO -- omniauth: (google_oauth2) Callback phase initiated.
I, [2018-04-24T20:57:56.719943 #14173] INFO -- omniauth: (google_oauth2) Callback phase initiated.
E, [2018-04-24T20:57:57.074575 #14173] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, invalid_grant: Code was already redeemed.
{
"error" : "invalid_grant",
"error_description" : "Code was already redeemed."
}
Processing by Devise::OmniauthCallbacksController#failure as HTML
Parameters: {"state"=>"xxxxxxxx", "code"=>"xxxxxxx"}
Redirected to http://localhost:3000/auth/sign_in
或者
Started GET "/omniauth/google_oauth2/callback?state=xxxxxx&code=xxxxxx" for 127.0.0.1 at 2018-04-24 21:13:46 +0800
I, [2018-04-24T21:13:46.268484 #14173] INFO -- omniauth: (google_oauth2) Callback phase initiated.
I, [2018-04-24T21:13:47.609079 #14173] INFO -- omniauth: (google_oauth2) Callback phase initiated.
E, [2018-04-24T21:13:47.978029 #14173] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, invalid_grant: Bad Request
{
"error" : "invalid_grant",
"error_description" : "Bad Request"
}
Processing by Devise::OmniauthCallbacksController#failure as HTML
Parameters: {"state"=>"xxxxxx", "code"=>"xxxxxx"}
Redirected to http://localhost:3000/auth/sign_in
这里还有以下配置: 设计.rb:
config.omniauth :google_oauth2,
'xxxx.apps.googleusercontent.com',
'client_secret',
provider_ignores_state: true,
:access_type => 'offline'
omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2,
'xxxxx.apps.googleusercontent.com',
'client_secret',
provider_ignores_state: true,
:access_type => 'offline'
end
如果您对根本原因有任何了解,请随时更新,
如果您面临 OAuth2::AccessToken.from_hash:
hash
包含多个“令牌”密钥 (["access_token", "id_token"]);使用“access_token”。
D,[2024-06-20T19:17:06.174097 #23685]调试--omniauth:(google_oauth2)回调阶段启动。
E,[2024-06-20T19:17:06.341526 #23685]错误--omniauth:(google_oauth2)身份验证失败! invalid_credentials:OAuth2 ::错误,invalid_grant:错误请求
我在 application.rb 和 devise.rb 中都使用了 client_id 和 client_secret 。 我删除了application.rb中的一个。 该问题已得到解决。 现在我还有另一个问题需要解决,再见。