Getting birthday,gender and photo from google UserInfo

532 views Asked by At

I got this code on below link.

How to get userinfo with google-api after succesfully been authenticated?

     GoogleCredential credential2 = new GoogleCredential.Builder()
            .setTransport(TRANSPORT).setJsonFactory(JSON_FACTORY)
            .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
            .setRequestInitializer((new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest request)
                        throws IOException {
                    request.getHeaders().setAuthorization("Bearer ".concat(tok));
                }
            }))
            .build();

    Oauth2 userInfoService = new Oauth2.Builder(TRANSPORT,
            JSON_FACTORY, credential2.getRequestInitializer())
            .setApplicationName(APPLICATION_NAME).build();

    Userinfo userInfo = userInfoService.userinfo().get().execute();
    logger.warn("User email: {}", userInfo.getEmail());
    logger.warn("User gender: {}", userInfo.getGender());
    logger.warn("User complet name: {} - {}", userInfo.getFamilyName(),     userInfo.getName());

I have some below question.!

1) can you give me steps..? what is the first step before getting the GoogleCredential..? How to get the token..?

2)I am using the "Google API Client Library for Java, version 1.16.0-rc" library. In that jar files , I am not getting "Oauth2" class..? Which version client library i need to take..?

3)If anyone has sample example with jar ...Please replay me.

1

There are 1 answers

0
Nick On

The link you're using as a reference is for after the user has been successfully authenticated. That means their user already has a code. If you are unsure of how to get one, you should probably first focus on getting the OAuth flow and wait to perform the API call after.

There are a number of good resources for showing you how to perform the OAuth flow (getting a code, exchanging it for a token, persisting the token, etc) using the Google API Client Library. One of my favorites is the Quickstart tool, which will generate a starter application for you that has the OAuth code already implemented.

You can try that out here: https://developers.google.com/quickstart/

(Note that it depends on Maven to handle the jars/dependencies)

You can also check out some of the library's samples here: https://code.google.com/p/google-api-java-client/source/browse/?repo=samples