如何使用JAVA库在Google API上获取用户个人资料? [英] How to get user profile on Google API using the JAVA library?

查看:161
本文介绍了如何使用JAVA库在Google API上获取用户个人资料?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Google API 有一个Java ClientRequest,它返回一个包含基于access_token的配置文件的json。网址为:

https:// www.googleapis.com/oauth2/v1/userinfo?access_token=ya29.1.AADtN_VALuuUTBm8ENfNTz8s ...



而回应是:

 
{
id:111223344556677889900,
email:myemail@gmail.com,
verified_email:true,
name:我的名字,
given_name:我的,
family_name:名称,
链接:plus.google.com/111223344556677889900,
picture:photo.jpg,
gender:male,
locale:en
}

一些观点:

strong>我想使用java库来避免挂载http请求,保留google服务器url和其他小事。

2 我不需要授权因为此时我的方法会接收访问令牌(所有的oauth ste ps之前完成)。
3 在这种方法中,我们没有(也不需要)客户端ID和密码。

4 我不需要Google+范围。其实我更喜欢不去那里。到目前为止,只有使用Plus库才能找到示例。



总而言之,我需要在google api java库中完全等价于现在使用的http请求。 b
$ b

提前非常感谢!

解决方案

I希望这有助于

  GoogleCredential凭证=新的GoogleCredential()。setAccessToken(accessToken); 
Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(),new JacksonFactory(),凭证).setApplicationName(
Oauth2)。build();
Userinfoplus userinfo = oauth2.userinfo()。get()。execute();
userinfo.toPrettyString();


I have a Java ClientRequest to the Google API that returns a json containing the profile based in an access_token. The URL is:
https://www.googleapis.com/oauth2/v1/userinfo?access_token=ya29.1.AADtN_VALuuUTBm8ENfNTz8s...

And the response is:

{
    "id": "111223344556677889900",
    "email": "myemail@gmail.com",
    "verified_email": true,
    "name": "My Name",
    "given_name": "My",
    "family_name": "Name",
    "link": "plus.google.com/111223344556677889900",
    "picture": "photo.jpg",
    "gender": "male",
    "locale": "en"
}

Some points:

1 I'd like to use the java library to avoid mount the http request, keep the google server url and another minor things.
2 I don't need the authorization's steps because at this point my method receives the access token (all the oauth steps are done before).
3 In this method we don't have (and so far don't need) the client id and secret.
4 I don't need the Google+ scope. Actually I prefer don't go there. So far only found examples using the Plus library.

In summary I need something in the google api java library exactly equivalent to the http request used nowadays.

Thank you very much in advance!

解决方案

I hope this helps

GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);   
 Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName(
          "Oauth2").build();
 Userinfoplus userinfo = oauth2.userinfo().get().execute();
 userinfo.toPrettyString();

这篇关于如何使用JAVA库在Google API上获取用户个人资料?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆