Ruby-on-rails 为什么 Google Oauth 在我的 Rails 应用程序中返回“无效的 redirect_urI”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10215863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is Google Oauth returning `invalid redirect_urI` in my Rails app?
提问by Andy Harvey
I'm adding Google Oauth2 to a Rails app, but have been unable to get past the early stages.
我正在将 Google Oauth2 添加到 Rails 应用程序中,但一直无法通过早期阶段。
I've set up an app, and defined client ID and secret.But I'm getting Invalid parameter value for redirect_uri: Non-public domains not allowed: http://localhost/path/to/callback
我已经设置了一个应用程序,并定义了客户端 ID 和密码。但是我得到了 Invalid parameter value for redirect_uri: Non-public domains not allowed: http://localhost/path/to/callback
What does this mean? Is this because I'm testing on a local dev environment?
这是什么意思?这是因为我正在本地开发环境中进行测试吗?
Thanks for any ideas.
感谢您的任何想法。
EDIT
编辑
This might be because the app's URI differs from the sending URI. But when I go to Google and try to authorize the path to my dev app, I get OAuth2 redirect is invalid. Is this a limitation of using a locally hosted app?
这可能是因为应用程序的 URI 与发送 URI 不同。但是当我去谷歌并尝试授权我的开发应用程序的路径时,我得到OAuth2 redirect is invalid. 这是使用本地托管应用程序的限制吗?
EDIT 2
编辑 2
The request I'm generating looks like this:
我生成的请求如下所示:
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=###########&redirect_uri=http%3A%2F%2Fmyapp.dev%2Fusers%2Fauth%2Fgoogle_oauth2%2Fcallback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&approval_prompt=&access_type=offline
Is this correct. I've tried this with client_id including and excluding the .apps.googleusercontent.comsection. Neither seems to work.
这样对吗。我已经尝试过使用 client_id 包括和排除该.apps.googleusercontent.com部分。两者似乎都不起作用。
采纳答案by Andy Harvey
For anyone else finding this, my problem was combination of several things.
对于其他发现此问题的人,我的问题是几件事的组合。
I could not get callbacks to work on a local machine. The console API console suggests it is possible, but I couldn't get it working. Not sure of this is down to the service, or to my network/ connection/ firewall/ etc.
In the API console you must specify the full redirect URI, not just the root url back to your app.
Google lists scope parameters here https://developers.google.com/gdata/faq#AuthScopes. It seems this information is outdated, and the correct format for these is now https://www.googleapis.com/auth/plus.me, https://www.googleapis.com/auth/youtube, etc. Maybe someone else can confirm this?
我无法在本地机器上获得回调。控制台 API 控制台表明这是可能的,但我无法让它工作。不确定这与服务有关,还是与我的网络/连接/防火墙/等有关。
在 API 控制台中,您必须指定完整的重定向 URI,而不仅仅是返回应用程序的根 url。
Google 在此处列出了范围参数https://developers.google.com/gdata/faq#AuthScopes。似乎此信息已过时,这些信息的正确格式现在是https://www.googleapis.com/auth/plus.me、https://www.googleapis.com/auth/youtube等。也许是其他人可以确认吗?
I had a few other things going on as well. I had written a full explanation on another of my questions here on SO. Unfortunately my comments were deleted instead of moved by a moderator because I posted in the wrong place. I can't now remember all that I wrote. For others facing similar issue, feel free to comment, it may jog my memory so I can offer some ideas.
我还有其他一些事情要做。我在 SO 上写了关于我的另一个问题的完整解释。不幸的是,我的评论被删除而不是被版主移动,因为我发错了地方。我现在不记得我写的所有内容了。对于其他面临类似问题的人,请随时发表评论,它可能会唤醒我的记忆,以便我提供一些想法。
回答by Oisin
I was getting the redirect error for my python / tornado app running on ubuntu. Using localhost didn't work as the accepted answer highlighted. Google wants a public domain.
我在 ubuntu 上运行的 python/tornado 应用程序出现重定向错误。使用 localhost 不起作用,因为已接受的答案突出显示。谷歌想要一个公共领域。
My solution was to piggyback "example.com" which is public and create a sub domain in my /etc/hosts file. The sub domain would work on my local dev box and google would be happy with the example.com domain. I registering the redirects via the google console and the redirect worked successfully for me.
我的解决方案是搭载公共的“example.com”并在我的 /etc/hosts 文件中创建一个子域。子域可以在我的本地开发箱上工作,而 google 会对 example.com 域感到满意。我通过谷歌控制台注册了重定向,重定向对我来说成功了。
I added the following to my /etc/hosts:
我在 /etc/hosts 中添加了以下内容:
192.168.33.100 devbox devbox.example.com
In my case the IP was that of my machine. I could also have used 127.0.0.1 instead.
在我的情况下,IP 是我机器的 IP。我也可以使用 127.0.0.1 代替。
My Google API console (https://code.google.com/apis/console) set up for a new client ID was:
我为新客户端 ID 设置的Google API 控制台 ( https://code.google.com/apis/console) 是:
- "Application Type: Web Application".
- Via "Your site or hostname (more options)":
- In "Authorized Redirect URIs" I entered http://devbox.example.com/
- In "Authorized JavaScript Origins" I entered http://devbox.example.com/
- “应用程序类型:Web 应用程序”。
- 通过“您的站点或主机名(更多选项)”:
- 在“授权重定向 URI”中,我输入了http://devbox.example.com/
- 在“Authorized JavaScript Origins”中,我输入了http://devbox.example.com/
回答by Happynoff
Using xip.io you can provide a public url to redirect to like http://your_pow_app.192.168.0.1.xip.io/user/auth/google_oauth2/callback
使用 xip.io,您可以提供一个公共 url 来重定向到http://your_pow_app.192.168.0.1.xip.io/user/auth/google_oauth2/callback
Tested and working.
测试和工作。
回答by Tyson
I used my public hostname. It helps if you have a static IP address. I used http://www.displaymyhostname.com/to get my hostname. I plugged it straight into the Authorized JavaScript originsfield when I created a new Web Application Client ID.
我使用了我的公共主机名。如果您有静态 IP 地址,这会有所帮助。我使用http://www.displaymyhostname.com/来获取我的主机名。当我创建一个新的Web 应用程序客户端 ID时,我将它直接插入到Authorized JavaScript origins字段中。
P.S. My hostname looked something like this: 111.111.111.111.static.exetel.com.au
PS 我的主机名看起来像这样:111.111.111.111.static.exetel.com.au
This is my answer to a related question https://stackoverflow.com/a/23517146/1320083
这是我对相关问题的回答https://stackoverflow.com/a/23517146/1320083
回答by Venu Gopal Tewari
Error redirect_uri_mismatch - This error can occur when you entered an incorrect bundle ID in your Google Developers Console project that does not match your app's bundle ID. Check that your client ID and bundle ID match the values that are displayed in the Developers Console.
错误 redirect_uri_mismatch - 当您在 Google Developers Console 项目中输入的包 ID 与应用的包 ID 不匹配时,可能会发生此错误。检查您的客户端 ID 和捆绑 ID 是否与 Developers Console 中显示的值匹配。
回答by Lanklaas
For me it was the function that built up the google url. I put line breaks in the string, once I removed the line breaks the url worked again.
对我来说,它是构建 google url 的函数。我在字符串中放置了换行符,一旦我删除了换行符,url 就会再次工作。
回答by cdavidyoung
I faced this issue but found it was really not an issue. As explained in my blogyou can use the public redirect URI even when you are working on your localhost development machine. Google will just return the authorization code to the public URI where you can cut and paste it to your local machine.
我遇到了这个问题,但发现这真的不是问题。正如我在博客中所解释的,即使您在 localhost 开发机器上工作,您也可以使用公共重定向 URI。Google 只会将授权代码返回到公共 URI,您可以在其中将其剪切并粘贴到本地计算机。

