Google API Java客户端 - 获取用户信息

时间:2022-12-16 15:25:15

I'm having trouble figuring out a seemingly simple task in the Google API Java client library version 1.12.0-beta. I can authenticate with OAuth2 and can retrieve and manipulate parts of Google Drive that I need for my application. However, I would like to follow the Google best practices and display basic user information at the top of my app.

我在Google API Java客户端库版本1.12.0-beta中找出看似简单的任务时遇到了麻烦。我可以使用OAuth2进行身份验证,并可以检索和操作我的应用程序所需的Google云端硬盘部分。但是,我想遵循Google最佳做法,并在我的应用顶部显示基本用户信息。

I have searched through the maze of documents Google provides and have searched many other sites as well and cannot seem to find what I need. I looked in to the Userinfo API suggested on the best practices page. As far as I can see, it should be a part of the java client I'm using, but it is not. I even found a full method example outlining exactly how I might get user info. The class it refers to - Userinfo - does not appear to be a part of any of the libs included in the client library I'm using. I searched further to see if I was missing a separate download that would include the OAuth services Java client.

我搜索了谷歌提供的文件迷宫,并搜索了许多其他网站,似乎无法找到我需要的东西。我查看了最佳实践页面上建议的Userinfo API。据我所知,它应该是我正在使用的java客户端的一部分,但事实并非如此。我甚至找到了一个完整的方法示例,概述了我如何获取用户信息。它引用的类 - Userinfo - 似乎不是我正在使用的客户端库中包含的任何库的一部分。我进一步搜索,看看我是否错过了包含OAuth服务Java客户端的单独下载。

I think the major issue I'm having is finding relevant information for the current version of the Java client library. Has anyone else ran across this issue? I would greatly appreciate any pointers on finding out how to get basic user information.

我认为我遇到的主要问题是找到当前版本的Java客户端库的相关信息。还有其他人遇到过这个问题吗?我非常感谢有关如何获取基本用户信息的任何指示。

Thanks for your help.

谢谢你的帮助。

3 个解决方案

#1


0  

I think you are mixing up the Drip API and the OAuth API.

我认为你混淆了Drip API和OAuth API。

User information can be got from the Drive API by:

用户信息可以通过以下方式从Drive API获取:

(where service is your instance of com.google.api.services.drive.Drive)

(其中service是com.google.api.services.drive.Drive的实例)

About about = service.about().get().execute();
System.out.println("Current user name: " + about.getName());
System.out.println("Root folder ID: " + about.getRootFolderId());
System.out.println("Total quota (bytes): " + about.getQuotaBytesTotal());
System.out.println("Used quota (bytes): " + about.getQuotaBytesUsed());

From https://developers.google.com/drive/v2/reference/about/get

来自https://developers.google.com/drive/v2/reference/about/get

#2


0  

For anyone looking, as I was, you need:

对于任何看起来像我一样的人,你需要:

 <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-oauth2</artifactId>
</dependency>

Pro tip: when you have a Java class name, go to Maven Central, Advanced Search, and seearch for the class name. It will list all libraries that contain the class. You can either use the fully qualified name or just the class name. Even if you do not use maven, you can download the jar file from there.

专业提示:当您拥有Java类名称时,请转到Maven Central,高级搜索和类名称的seearch。它将列出包含该类的所有库。您可以使用完全限定名称,也可以只使用类名。即使您不使用maven,也可以从那里下载jar文件。

#3


0  

Here is an example of getting userinfo using OAuth 2 in java If you add Google Drive to SCOPE(eg. https://www.googleapis.com/auth/drive.file), you can even access the Google Drive API

以下是在java中使用OAuth 2获取userinfo的示例如果您将Google云端硬盘添加到SCOPE(例如https://www.googleapis.com/auth/drive.file),您甚至可以访问Google Drive API

Full example
https://github.com/riversun/google-login-servlet-example-simple

完整示例https://github.com/riversun/google-login-servlet-example-simple

In the servlet

在servlet中

        GoogleCredential credential = OAuthSession.getInstance().createCredential(req);

        Oauth2 oauth2 = new Oauth2.Builder(
                new com.google.api.client.http.javanet.NetHttpTransport(),
                new com.google.api.client.json.jackson2.JacksonFactory(),
                credential).build();

        // Get userInfo using credential
        Userinfoplus userInfo = oauth2.userinfo().get().execute();

In the OAuthFilter

在OAuthFilter中

    // Return OAuth2 scope you want to be granted to by users
    @Override
    protected List<String> getScopes() {

        final String OAUTH2_SCOPE_MAIL = "email";
        final String OAUTH2_SCOPE_USERINFO_PROFILE = "https://www.googleapis.com/auth/userinfo.profile";

        return Arrays.asList(OAUTH2_SCOPE_MAIL, OAUTH2_SCOPE_USERINFO_PROFILE);}

#1


0  

I think you are mixing up the Drip API and the OAuth API.

我认为你混淆了Drip API和OAuth API。

User information can be got from the Drive API by:

用户信息可以通过以下方式从Drive API获取:

(where service is your instance of com.google.api.services.drive.Drive)

(其中service是com.google.api.services.drive.Drive的实例)

About about = service.about().get().execute();
System.out.println("Current user name: " + about.getName());
System.out.println("Root folder ID: " + about.getRootFolderId());
System.out.println("Total quota (bytes): " + about.getQuotaBytesTotal());
System.out.println("Used quota (bytes): " + about.getQuotaBytesUsed());

From https://developers.google.com/drive/v2/reference/about/get

来自https://developers.google.com/drive/v2/reference/about/get

#2


0  

For anyone looking, as I was, you need:

对于任何看起来像我一样的人,你需要:

 <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-oauth2</artifactId>
</dependency>

Pro tip: when you have a Java class name, go to Maven Central, Advanced Search, and seearch for the class name. It will list all libraries that contain the class. You can either use the fully qualified name or just the class name. Even if you do not use maven, you can download the jar file from there.

专业提示:当您拥有Java类名称时,请转到Maven Central,高级搜索和类名称的seearch。它将列出包含该类的所有库。您可以使用完全限定名称,也可以只使用类名。即使您不使用maven,也可以从那里下载jar文件。

#3


0  

Here is an example of getting userinfo using OAuth 2 in java If you add Google Drive to SCOPE(eg. https://www.googleapis.com/auth/drive.file), you can even access the Google Drive API

以下是在java中使用OAuth 2获取userinfo的示例如果您将Google云端硬盘添加到SCOPE(例如https://www.googleapis.com/auth/drive.file),您甚至可以访问Google Drive API

Full example
https://github.com/riversun/google-login-servlet-example-simple

完整示例https://github.com/riversun/google-login-servlet-example-simple

In the servlet

在servlet中

        GoogleCredential credential = OAuthSession.getInstance().createCredential(req);

        Oauth2 oauth2 = new Oauth2.Builder(
                new com.google.api.client.http.javanet.NetHttpTransport(),
                new com.google.api.client.json.jackson2.JacksonFactory(),
                credential).build();

        // Get userInfo using credential
        Userinfoplus userInfo = oauth2.userinfo().get().execute();

In the OAuthFilter

在OAuthFilter中

    // Return OAuth2 scope you want to be granted to by users
    @Override
    protected List<String> getScopes() {

        final String OAUTH2_SCOPE_MAIL = "email";
        final String OAUTH2_SCOPE_USERINFO_PROFILE = "https://www.googleapis.com/auth/userinfo.profile";

        return Arrays.asList(OAUTH2_SCOPE_MAIL, OAUTH2_SCOPE_USERINFO_PROFILE);}