简体   繁体   English

如何使用 JAVA 库在 Google API 上获取用户配置文件?

[英]How to get user profile on Google API using the JAVA library?

I have a Java ClientRequest to the Google API that returns a json containing the profile based in an access_token.我有一个 Java ClientRequest 到Google API ,它返回一个 json,其中包含基于 access_token 的配置文件。 The URL is: URL 是:
https://www.googleapis.com/oauth2/v1/userinfo?access_token=ya29.1.AADtN_VALuuUTBm8ENfNTz8s ... 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. 1我想使用 java 库来避免挂载 http 请求,保留谷歌服务器 url 和其他小事。
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). 2我不需要授权的步骤,因为此时我的方法接收到访问令牌(之前完成了所有 oauth 步骤)。
3 In this method we don't have (and so far don't need) the client id and secret. 3在这种方法中,我们没有(目前也不需要)客户端 ID 和密码。
4 I don't need the Google+ scope. 4我不需要 Google+ scope。 Actually I prefer don't go there.其实我更喜欢不要 go 那里。 So far only found examples using the Plus library.到目前为止,只找到了使用 Plus 库的示例。

In summary I need something in the google api java library exactly equivalent to the http request used nowadays.总之,我需要谷歌 api java 库中的一些东西,这与现在使用的 http 请求完全相同。

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();

Complementing with more info, since I don't have enough reputation to comment on the above answer:补充更多信息,因为我没有足够的声誉来评论上述答案:

After adding添加后

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-oauth2</artifactId>     
    <version>v2-rev65-1.17.0-rc</version>
</dependency>

to your pom.xml file, the following line到您的pom.xml文件,以下行

Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("Oauth2").build();

could raise "JacksonFactory cannot be resolved to a type", and if this happens, you can replace it with new GsonFactory() (from com.google.api.client.json.gson ), and it'll work. could raise "JacksonFactory cannot be resolved to a type", and if this happens, you can replace it with new GsonFactory() (from com.google.api.client.json.gson ), and it'll work.

Now, about how to get an accessToken : it comes inside com.google.auth.oauth2.UserCredentials ( com.google.auth.oauth2.UserCredentials ), which you can get by calling com.google.auth.oauth2.UserAuthorizer.getCredentialsFromCode . Now, about how to get an accessToken : it comes inside com.google.auth.oauth2.UserCredentials ( com.google.auth.oauth2.UserCredentials ), which you can get by calling com.google.auth.oauth2.UserAuthorizer.getCredentialsFromCode .

You can build a UserAuthorizer by calling您可以通过调用构建UserAuthorizer

UserAuthorizer
    .newBuilder()
    .setClientId(ClientId.of(<client id>, <client secret>))
    .setScopes(<scopes>)
    .build()

You can get the <client id> and the <client secret> by reading the OAuth Client ID file created on your google cloud project dashboard under credentials .您可以通过阅读在您的谷歌云项目仪表板上的凭据创建的 OAuth 客户端 ID 文件来获取<client id><client secret>

And lastly, an example of how to read the file using com.google.auth.oauth2.ClientId :最后,一个如何使用com.google.auth.oauth2.ClientId读取文件的示例:

ClientId parsedClient = ClientId.fromStream(new FileInputStream("key.json"))

I'm a bit late here, but I hope this helps someone.我在这里有点晚了,但我希望这对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在应用程序外部获取 AWS Connect 用户的安全配置文件? - How to get security profile of AWS Connect user outside of the application? 如何使用 Google Cloud API 获取给定存储桶中的文件夹列表 - How to get list of folders in a given bucket using Google Cloud API 如何使用 google pubsub java 库处理消息确认期间的错误? - How to handle errors during message acknowledgement using google pubsub java library? 如何使用 Firebase Firestore 在 Futter 中显示用户名和个人资料图片 - How to show user name and profile picture in Futter using Firebase Firestore 如何使用 OS Login API 从 Google Compute 实例中删除或获取 SSH 公钥 - How to delete or get SSH Public keys from Google Compute instances using OS Login API 如何使用 Api Key In Java Language 设置 Google Recaptcha Enterprise 验证? - How to set Google Recaptcha Enterprise verification Using Api Key In Java Language? 如何将 sagemaker createApp 添加到用户配置文件执行角色? - How to add sagemaker createApp to user profile executionrole? 用户 Google Api 与 Firebase 身份验证 - User Google Api with Firebase auth 尝试使用 cloudrun 实例上的 unix 套接字连接到 Google CloudSQL 时,如何获取 IAM 用户的用户名? - How do I get the username for the IAM user when trying to connect to Google CloudSQL using a unix socket on a cloudrun instance? 如何通过 google-auth-library 生成 JWT 令牌以将其用于 api 网关? - How do I generate JWT token via google-auth-library to use it for api gateway?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM