3pillarlabs / socialauth

Java Library for authentication, getting profile, contacts and updating status on Google, Yahoo, Facebook, Twitter, LinkedIn, and many more providers.
https://3pillarlabs.github.io/socialauth/
MIT License
249 stars 141 forks source link

How to get user profile? #94

Open franzisk opened 8 years ago

franzisk commented 8 years ago

Hi there,

I am trying to recover user profile but after doing the call and getting the Manager (see below), when I try to get profile by getting the the provider with the manager I always get the exception:

SocialAuthConfig config = SocialAuthConfig.getDefault();
config.load(props);
manager = new SocialAuthManager();
manager.setSocialAuthConfig(config); // this is NOT null
...

/* alter calling facebook and returning the my URL I try */
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
Map<String, String> map = SocialAuthUtil.getRequestParametersMap(request);

if (this.manager != null) {
    //AuthProvider provider = manager.connect(SocialAuthUtil.getRequestParametersMap(request));

    AuthProvider provider = manager.connect(map); // ERROR here
    this.profile = provider.getUserProfile();

    // Do what you want with the data (e.g. persist to the database, etc.)
    System.out.println("User's Social profile: " + profile);

    // Redirect the user back to where they have been before logging in
    FacesContext.getCurrentInstance().getExternalContext().redirect(originalURL);
} else {
    FacesContext.getCurrentInstance().getExternalContext().redirect(externalContext.getRequestContextPath() + "/pages/home.jsf");
}

Exception: org.brickred.socialauth.exception.SocialAuthException: Verification code is null

What is this "Verification code"?

When Facebook redirects back to my URL it comes with this: /home.jsf?code=AQC1B1a4QDhkfQVtzCmXarf_fR4ZKbCVczKJ1kdDdcegN8l37C8uJ7afKNKSDWbTd0kupObDVMmJznfBGGhhCntSblzdFlaphIGfBpSEpGAZhbLSbckjQ6EIuBEbaauOb2yHUyUisiCKAwpfdCYt-NBN990HpuSo0UHeeUcIy34TbT2Yy5H6-mKgc-LD9b0idDEEfCnty6qsU71uklLPKgq7n4FvZe00-pRY5wOz2Pr-TORdOh62HI7GbjabLvUopl46cv6S2sG4Jw-RZU26w1GC9t7LIIbNXVRfaiiBedFm_Anguvl-9TcZWBTVen-YDWrQfHos4Mq5RTIYh5DIz#=_

What should I do with this code?

Appreciate any help.

Thanks

Kaiser1989 commented 7 years ago

Access your parameter map and try to get the value for "code" key and check if it's null. Your error says, that it is null. Otherwise you can access code value manuelly and create a connection like this:

AccessGrant token = manager.createAccessGrant(PROVIDER, code, REDIRECT_URL);
AuthProvider provider = manager.connect(token);

Provider is your used provider, code the the value you ask for, and redirect url, the url back to your webapp

or if manager is already initialized:

Map<String,String> map = new HashMap<String, String>();
map.put("code", code);      
AuthProvider provider = manager.connect(map);
linxianying commented 6 years ago

Meet the same problem. Is there anyone who solved it? Thanks