我找到了答案。我的目标也是这个。我们可以成功地做到这一点:)
你需要:
https://www.googleapis.com/auth/userinfo.email同意
访问令牌
在以上两个之后,只需向https://www.googleapis.com/oauth2/v2/userinfo发出 HTTP GET 请求
但是添加了标题“授权:承载<accessToken>
”
你可以通过很多方式做到这一点。我的方式是自定义 REST 调用
package com.google.plus.samples.photohunt.custom;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientGet {
// http://localhost:8080/RESTfulExample/json/product/get
public static void makeRequest(String access_token) {
try {
URL url = new URL("https://www.googleapis.com/oauth2/v2/userinfo");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer "+access_token);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("REST call made. Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}