5

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.

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.

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.

Thanks for your help.

Jerome
  • 2,104
  • 1
  • 17
  • 31
user1893454
  • 51
  • 1
  • 2

3 Answers3

0

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

User information can be got from the Drive API by:

(where service is your instance of 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

deive
  • 862
  • 7
  • 19
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.

Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49
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

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

In the 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

    // 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);}
riversun
  • 758
  • 8
  • 12