Java Google OAUTH:请求中的重定向 URI 与注册的重定向 URI 不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22595174/
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
Google OAUTH: The redirect URI in the request did not match a registered redirect URI
提问by Denees
I am trying to make an upload to YouTube from my Java based web app, I spent a few days to understand what and where is the problem and I cannot get it, for now I am pulling my hair out off my head.
我试图从我的基于 Java 的网络应用程序上传到 YouTube,我花了几天时间来了解问题出在哪里,但我无法理解,现在我正在把头发从头上拔下来。
I registered my web app in Google Console, so I got a pair of Client ID and Secret and a possibility to download JSON type file with my config.
我在 Google Console 中注册了我的 Web 应用程序,因此我获得了一对客户端 ID 和密钥,并且可以使用我的配置下载 JSON 类型文件。
So here is the config:
所以这是配置:
{
"web":{
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"client_secret":"***",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"client_email":"***",
"redirect_uris":["http://localhost:8080/WEBAPP/youtube-callback.html","http://www.WEBAPP.md/youtube-callback.html"],
"client_x509_cert_url":"***",
"client_id":"***",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"javascript_origins":["http://www.WEBAPP.md/"]
}
}
How is possible that I am getting the default URL from Google?
我怎么可能从 Google 获取默认 URL?
The redirect URI in the request: http://localhost:8080/Callback did not match a registered redirect URI
The redirect URI in the request: http://localhost:8080/Callback did not match a registered redirect URI
It always gives me the default http://localhost:8080/Callback
URL instead of mine.
它总是给我默认的http://localhost:8080/Callback
URL 而不是我的。
And IDE console shows me that:
IDE 控制台告诉我:
Please open the following address in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=***&redirect_uri=http://localhost:8080/Callback&response_type=code&scope=https://www.googleapis.com/auth/youtube.upload
Attempting to open that address in the default browser now...
Please open the following address in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=***&redirect_uri=http://localhost:8080/Callback&response_type=code&scope=https://www.googleapis.com/auth/youtube.upload
Attempting to open that address in the default browser now...
I am using the last version of dependencies: google-api-services-youtube v3-rev99-1.17.0-rcand google-api-services-youtubeAnalytics v1-rev35-1.17.0-rc
我正在使用最新版本的依赖项: google-api-services-youtube v3-rev99-1.17.0-rc和google-api-services-youtubeAnalytics v1-rev35-1.17.0-rc
采纳答案by jlmcdonald
When your browser redirects the user to Google's oAuth page, are you passing as a parameter the redirect URI you want Google's server to return to with the token response? Setting a redirect URI in the console is not a way of telling Google where to go when a login attempt comes in, but rather it's a way of telling Google what the allowed redirect URIs are (so if someone else writes a web app with your client ID but a different redirect URI it will be disallowed); your web app should, when someone clicks the "login" button, send the browser to:
当您的浏览器将用户重定向到 Google 的 oAuth 页面时,您是否将希望 Google 的服务器与令牌响应一起返回的重定向 URI 作为参数传递?在控制台中设置重定向 URI 并不是在登录尝试进入时告诉 Google 去哪里的一种方式,而是一种告诉 Google 允许的重定向 URI 是什么的方式(因此,如果其他人与您的客户端一起编写了 Web 应用程序) ID 但不同的重定向 URI 将被禁止);当有人点击“登录”按钮时,您的网络应用程序应该将浏览器发送到:
https://accounts.google.com/o/oauth2/auth?client_id=XXXXX&redirect_uri=http://localhost:8080/WEBAPP/youtube-callback.html&response_type=code&scope=https://www.googleapis.com/auth/youtube.upload
(the callback URI passed as a parameter must be url-encoded, btw).
(作为参数传递的回调 URI 必须是 url 编码的,顺便说一句)。
When Google's server gets authorization from the user, then, it'll redirect the browser to whatever you sent in as the redirect_uri
. It'll include in that request the token as a parameter, so your callback page can then validate the token, get an access token, and move on to the other parts of your app.
当 Google 的服务器获得用户的授权后,它会将浏览器重定向到您作为redirect_uri
. 它会将令牌作为参数包含在该请求中,因此您的回调页面可以验证该令牌、获取访问令牌,并继续执行应用程序的其他部分。
If you visit:
如果您访问:
http://code.google.com/p/google-api-java-client/wiki/OAuth2#Authorization_Code_Flow
http://code.google.com/p/google-api-java-client/wiki/OAuth2#Authorization_Code_Flow
You can see better samples of the java client there, demonstrating that you have to override the getRedirectUri
method to specify your callback path so the default isn't used.
您可以在那里看到更好的 java 客户端示例,证明您必须覆盖该getRedirectUri
方法以指定您的回调路径,以便不使用默认值。
The redirect URIs are in the client_secrets.json
file for multiple reasons ... one big one is so that the oAuth flow can verify that the redirect your app specifies matches what your app allows.
重定向 URI 位于client_secrets.json
文件中的原因有很多……其中一个重要原因是 oAuth 流可以验证您的应用指定的重定向是否与您的应用允许的重定向相匹配。
If you visit https://developers.google.com/api-client-library/java/apis/youtube/v3You can generate a sample application for yourself that's based directly off your app in the console, in which (again) the getRedirectUri method is overwritten to use your specific callbacks.
如果您访问https://developers.google.com/api-client-library/java/apis/youtube/v3您可以为自己生成一个示例应用程序,该应用程序直接基于您在控制台中的应用程序,其中(再次) getRedirectUri 方法被覆盖以使用您的特定回调。
回答by Ikai Lan
You need to go into the developer console and set
您需要进入开发者控制台并设置
http://localhost:8080/WEBAPP/youtube-callback.html
as your callback URL.
作为您的回调 URL。
This videois slightly outdated, as it shows the older Developer Console instead of the new one, however, the concepts should still apply. You need to find your project in the developer console and register a callback URL.
该视频有点过时,因为它显示的是旧版开发者控制台而不是新版,但是,这些概念仍然适用。您需要在开发者控制台中找到您的项目并注册一个回调 URL。
回答by Max
I think I encountered the same problem as you. I addressed this problem with the following steps:
我想我遇到了和你一样的问题。我通过以下步骤解决了这个问题:
1) Go to Google Developers Console
1) 前往谷歌开发者控制台
2) Set JavaScript origins:
2) 设置 JavaScript 来源:
3) Set Redirect URIs:
3) 设置重定向 URI:
回答by datalifenyc
I was able to get mine working using the following Client Credentials:
我能够使用以下客户端凭据让我的工作:
Authorized JavaScript origins
授权的 JavaScript 起源
http://localhost
http://localhost
Authorized redirect URIs
授权的重定向 URI
http://localhost:8090/oauth2callback
http://localhost:8090/oauth2callback
Note: I used port 8090 instead of 8080, but that doesn't matter as long as your python script uses the same port as your client_secret.json file.
注意:我使用了端口 8090 而不是 8080,但是只要您的 python 脚本使用与您的 client_secret.json 文件相同的端口,这并不重要。
Reference: Python Quickstart
参考:Python 快速入门
回答by ben_mj
I thought I had this configured but it turns out I set the URL in the wrong place. I followed the URL provided in the Google error page and added my URL here. Stupid mistake from my part, but easily done. Hope this helps
我以为我已经配置了这个,但事实证明我在错误的地方设置了 URL。我遵循了 Google 错误页面中提供的 URL,并在此处添加了我的 URL。我犯了一个愚蠢的错误,但很容易做到。希望这可以帮助