我可以使用 oath2 从 Google API 响应中获取用户信息。但我不知道如何单独获得这些回复。
我从 Google Api 得到的响应: *
Access token: ya29.AHES6ZQ3QxKxnfAzpZasdfd23423NuxJs29gMa39MXV551yMmyM5IgA
{
"id": "112361XXXXXXXXXXX",
"name": "Ansuman XX",
"given_name": "Ansuman",
"family_name": "Singh",
"link": "https://plus.google.com/XXXXXXXXXXXXXXXXX",
"gender": "male",
"birthday": "0000-03-18",
"locale": "en"
}
Original Token: ya29.AHES6ZQ3QxKxnfAzpZu0lYHYu8sdfsdafdgMa39MXV551yMmyM5IgA New Token: ya29.AHES6ZQ3QxKxnfdsfsdaYHYu8TNuxJs29gMa39MXV551yMmyM5IgA
但我只想将“id”和“name”单独保存在我的数据库表中。我怎样才能做到这一点?
通过使用下面的代码,我得到了上述响应/输出。
public static void main(String[] args) throws IOException
{
-------------------------
-------------------------
-------------------------
String accessToken = authResponse.accessToken;
GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken, TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken);
HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
System.out.println("Access token: " + authResponse.accessToken);
String url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + authResponse.accessToken;
final StringBuffer r = new StringBuffer();
final URL u = new URL(url);
final URLConnection uc = u.openConnection();
final int end = 1000;
InputStreamReader isr = null;
BufferedReader br = null;
isr = new InputStreamReader(uc.getInputStream());
br = new BufferedReader(isr);
final int chk = 0;
while ((url = br.readLine()) != null)
{
if ((chk >= 0) && ((chk < end)))
{
r.append(url).append('\n');
}
}
System.out.print("");
System.out.println();
System.out.print(" "+ r ); //this is printing at once but i want them individually
access.refreshToken();
System.out.println("Original Token: " + accessToken + " New Token: " + access.getAccessToken());
}