2

In my GWT application, I am using the com.google.api.gwt.oauth2.OAuth2 module to allow users to signin with OAuth. I have implemented a GoogleApi class to provide the OAuth login as follows:

public final class GoogleApi {
    private EventBus eventBus;

    private final ClientOAuth2Login oAuth2Login;
    private ClientGoogleApiRequestTransport requestTransport;

    private String accessToken;

    public GoogleApi(final EventBus eventBus, String clientId) {
        this.eventBus = eventBus;
        oAuth2Login = new ClientOAuth2Login(clientId);
    }

    ...

    public void login(final Receiver<String> callback) {
        oAuth2Login.login(new Receiver<String>() {
            @Override
            public void onSuccess(String response) {
                accessToken = response;
                callback.onSuccess(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                Window.alert(error.getMessage());
            }
        });
    }
    ...
}

I am calling this login() method as follows:

googleApi.login(new Receiver<String>() {
    @Override
    public void onSuccess(final String token) {
        // I want to get the logged in user's name here
    }

    @Override
    public void onFailure(ServerFailure error) {
        ...
    }
});

After successful login, I want to get the logged in user's full name. I know that I can get the user's name using the com.google.api.gwt.services.Plus module, but that requires users to be registered to Google Plus and throws an error if they're not.

How can I do this using OAuth in my GWT app? Basically, I want to be able to implement this solution in a GWT app, if possible.

Community
  • 1
  • 1
  • Have a look at https://code.google.com/p/gwt-google-apis/source/browse/trunk/apis/samples/plus/com/google/api/gwt/samples/plus/client/PlusEntryPoint.java or https://code.google.com/p/gwt-oauth2/source/browse/trunk/samples/multi/com/google/api/gwt/oauth2/samples/multi/client/OAuth2SampleEntryPoint.java depending on which library you use. – Thomas Broyer Apr 13 '13 at 09:22
  • Thank you for your response, Thomas. The code at your first link uses Google Plus to get the user's name & other info. The second does not have the code to retrieve the user's name. –  Apr 13 '13 at 10:41

1 Answers1

1

Have you see this? I thought it was quite useful. It is, however, a lot of code to achieve a simple result:

http://www.sw-engineering-candies.com/blog-1/howtogetuserinformationwithoauth2inagwtandgoogleappenginejavaapplication

D2TheC
  • 2,203
  • 20
  • 23
  • Thanks, Dirk. Your code uses AppEngine-specific API. It wont work on other hosts. –  May 14 '13 at 16:18