Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 获得;错误:重定向“uri不匹配”;使用google_oauth2_Ruby On Rails_Omniauth - Fatal编程技术网

Ruby on rails 获得;错误:重定向“uri不匹配”;使用google_oauth2

Ruby on rails 获得;错误:重定向“uri不匹配”;使用google_oauth2,ruby-on-rails,omniauth,Ruby On Rails,Omniauth,URL似乎是正确的(上次更新于昨天): 文件也包括: omniauth.rb: provider :google_oauth2, 'MY_CLIENT_ID.apps.googleusercontent.com', 'MY_CLIENT_SECRET', :scope => 'https://mail.google.com/mail/feed/atom/' Error: redirect_uri_mismatch The redirect URI in the r

URL似乎是正确的(上次更新于昨天):

文件也包括:

omniauth.rb:

provider :google_oauth2, 'MY_CLIENT_ID.apps.googleusercontent.com', 'MY_CLIENT_SECRET',
           :scope => 'https://mail.google.com/mail/feed/atom/'

Error: redirect_uri_mismatch
The redirect URI in the request: http://localhost:3000/auth/google_oauth2/callback did not match a registered redirect URI
match '/auth/:provider/callback', to: 'sessions#omniauth_create'
header.html.erb

<li><%= link_to "Sign in with Google", "auth/google_oauth2" %></li>
但我明白了:

> Error: redirect_uri_mismatch The redirect URI in the request:
> http://localhost:3000/auth/google_oauth2/callback did not match a
> registered redirect URI
(推特和Facebook OmniAuth运行良好)

不知道是什么问题。有什么建议可以解决这个问题吗

编辑

我将URI更改为
http
…:


但是仍然得到相同的错误。

看起来请求正在命中
http://localhost:3000/auth/google_oauth2/callback
,但与类似模式匹配的指定重定向URI用于
https
。添加
http://localhost:3000/auth/google_oauth2/callback
添加到重定向列表可能会解决该问题

编辑:另一个可能的修复方法是在相应的重定向URI中包含一个尾随的
/
,在这种情况下似乎有效。

vi config/initializers/omniauth.rb

OmniAuth.config.full_host='1https://localhost:3000'
Rails.application.config.middleware.use OmniAuth::Builder do
提供者:google_oauth2,“google_客户端id”,“google_客户端机密”
结束

分享一个非常简单的拷贝粘贴解决方案,它对我很有效

我复制了我在代码中指定为
redirectUri
的内容,即
“redirect\u uri”:http://127.0.0.1:3001/“
并将此
键的
值粘贴到请求授权重定向URI的Google设置中。这样可以确保两个参数相同


如果计算了
url
,我会
console.log()
在粘贴到google设置中之前,从控制台窗口复制它。

omniauth-oauth2
gem版本
1.4

临时修复方法是在
Gemfile

gem 'omniauth-oauth2', '~> 1.3.1'

我尝试了以上所有方法,但都不奏效。最后,我注意到在我的错误消息中,我的回拨略有不同。我有一个介于localhost:3000和auth之间的用户。不太清楚为什么

http://localhost:3000/users/auth/google_oauth2/callback

换了它,等了30分钟,它就成功了

谢谢。我也这么认为。我添加了一个
http://localhost:3000
重定向并更新omniath.rb。但是仍然得到同样的错误(请看我的编辑)。@alexchenco啊,我明白了。您是否尝试将
http
https
添加到URI列表中?我同意看起来你做的一切都是对的,但我通常默认在涉及OAuth时同时添加
http
https
URI。还有,很长一段时间,但是您是否尝试将尾部的
/
添加到URI中?因此,可能有四个URI(overkill,yes:):
http
https
,每个URI都包含尾随的前斜杠,不包含它。非常感谢!添加以添加尾随的
/
。非常奇怪,我的Rails应用程序没有尾随的
/
,谷歌提供的示例也没有包含它。@alexchenco太棒了!我甚至不想试图从理论上解释为什么,因为我会错的,但我已经不止一次遇到过同样的问题,所以我默认在那里抛出所有可能的组合。总有一天我会更有效率:)在重定向URI的末尾添加
?close
,这对我很有帮助。他们说要避免“谢谢”,所以我要说“爱你”,因为这样一个简单的解决方案:*试试这个答案,它对我很有用