使用发布在 Java 中的授权令牌从 Google 获取刷新令牌
Getting A Refresh Token From Google Using An Authorization Token Posted in Java
我已经阅读了很多帖子,所有我能找到的 Google 文档并尝试了以下的许多迭代,但仍然无法获得访问和刷新令牌。我确实获得了授权码,但似乎无法用它来交换访问和刷新令牌。
if(authCode == null || authCode.equals("")) {
String url = "https://accounts.google.com/o/oauth2/v2/auth?"
+ "scope=https://mail.google.com/&"
+ "response_type=code&"
+ "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"
+ "client_id=" + clientId +
"&access_type=offline";
URI uri = new URI(url);
logger.debug("URI for auth is: " + uri);
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(uri);
}
}
else {
logger.debug("Refreshing");
initRefreshToken();
}
有了这个,我得到了一个访问代码,我可以在我的属性中剪切和粘贴(只是测试并尝试让它首先工作)以获取刷新和访问令牌。
在initRefreshToken()方法中,源码是这样的:
if(refreshToken.equals("")) {
logger.debug("Getting refresh token");
HttpPost post = new HttpPost("https://oauth2.googleapis.com/token");
// add request parameter, form parameters
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("code", authCode));
urlParameters.add(new BasicNameValuePair("client_id", clientId));
urlParameters.add(new BasicNameValuePair("client_secret", clientSecret));
urlParameters.add(new BasicNameValuePair("redirect_uri", "http://localhost:8000/"));
urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
System.out.println("***** URL: " + urlParameters);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
}
如果这是第二次或以后使用该代码,将打印的内容是:
引用令牌:
***** URL: [code=4/1AY0e-g..., client_id=370...i1h2u1s.apps.googleusercontent.com, client_secret=bAOH..., redirect_uri=https://localhost:8000/, grant_type=authorization_code]
{
“错误”:“invalid_grant”,
"error_description": "错误请求"
}
如果代码是运行并且是第一次使用验证码,它会打印:
{
“错误”:“redirect_uri_mismatch”,
"error_description": "错误请求"
}
我在 Google 控制台中看到本地主机域有例外情况,因此无需注册它们。但是,如果需要注册它们,它不会让您注册它们,因为域必须是您拥有的顶级域才能注册它。因此,如何在 Java 中注册本地主机 and/or 以授权码交换访问和刷新令牌?
感谢您的帮助。
DaImTo 提供了一个很棒的视频,在那个视频和与之相关的博客 post 中,redirect_uri 被正确地列为:“urn:ietf:wg:oauth:2.0:哎呀”。我没有在文档中找到它,但是当我将它添加到我的源代码中时,我得到了访问和刷新令牌作为响应。非常感谢您的帮助,DaImTo。
我已经阅读了很多帖子,所有我能找到的 Google 文档并尝试了以下的许多迭代,但仍然无法获得访问和刷新令牌。我确实获得了授权码,但似乎无法用它来交换访问和刷新令牌。
if(authCode == null || authCode.equals("")) {
String url = "https://accounts.google.com/o/oauth2/v2/auth?"
+ "scope=https://mail.google.com/&"
+ "response_type=code&"
+ "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"
+ "client_id=" + clientId +
"&access_type=offline";
URI uri = new URI(url);
logger.debug("URI for auth is: " + uri);
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(uri);
}
}
else {
logger.debug("Refreshing");
initRefreshToken();
}
有了这个,我得到了一个访问代码,我可以在我的属性中剪切和粘贴(只是测试并尝试让它首先工作)以获取刷新和访问令牌。
在initRefreshToken()方法中,源码是这样的:
if(refreshToken.equals("")) {
logger.debug("Getting refresh token");
HttpPost post = new HttpPost("https://oauth2.googleapis.com/token");
// add request parameter, form parameters
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("code", authCode));
urlParameters.add(new BasicNameValuePair("client_id", clientId));
urlParameters.add(new BasicNameValuePair("client_secret", clientSecret));
urlParameters.add(new BasicNameValuePair("redirect_uri", "http://localhost:8000/"));
urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
System.out.println("***** URL: " + urlParameters);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
}
如果这是第二次或以后使用该代码,将打印的内容是:
引用令牌: ***** URL: [code=4/1AY0e-g..., client_id=370...i1h2u1s.apps.googleusercontent.com, client_secret=bAOH..., redirect_uri=https://localhost:8000/, grant_type=authorization_code]
{ “错误”:“invalid_grant”, "error_description": "错误请求" }
如果代码是运行并且是第一次使用验证码,它会打印:
{ “错误”:“redirect_uri_mismatch”, "error_description": "错误请求" }
我在 Google 控制台中看到本地主机域有例外情况,因此无需注册它们。但是,如果需要注册它们,它不会让您注册它们,因为域必须是您拥有的顶级域才能注册它。因此,如何在 Java 中注册本地主机 and/or 以授权码交换访问和刷新令牌?
感谢您的帮助。
DaImTo 提供了一个很棒的视频,在那个视频和与之相关的博客 post 中,redirect_uri 被正确地列为:“urn:ietf:wg:oauth:2.0:哎呀”。我没有在文档中找到它,但是当我将它添加到我的源代码中时,我得到了访问和刷新令牌作为响应。非常感谢您的帮助,DaImTo。