0

I have been working with social auth API in JSF, for getting an login with facebook, I dont know whether am creating a session properly. I have searched some stuffs form internet and doing it. Now i have caught up with some error.

according to social auth first we need to call a function, which helps to get the authentication URL from the Facebook, where i need to create a session and and set attributes to it, with the parameters from facebook once user logs in into the facebook.

so my first view page will just contain, a command button to call the respective function.

            <h:form><h:commandButton value="submit" action="#{socialNetworkAuthentication.facebookAuthentication}"></h:commandButton></h:form>

then the function which is called ...

                 public String facebookAuthentication(){

    try{


           //Create an instance of SocialAuthConfgi object
           SocialAuthConfig config = SocialAuthConfig.getDefault();

 String propUrl="oauth_consumer.properties";


          //load configuration. By default load the configuration from oauth_consumer.properties.
          //You can also pass input stream, properties object or properties file name.
           config.load(propUrl);

          //Create an instance of SocialAuthManager and set config
          SocialAuthManager manager = new SocialAuthManager();
          manager.setSocialAuthConfig(config);

          //URL of YOUR application which will be called after authentication
           String successUrl = "http://chennaivolunteers.com/ChennaiVolunteers/faces/cv/employee-profile.xhtml";


        // get Provider URL to which you should redirect for authentication.
          // id can have values "facebook", "twitter", "yahoo" etc. or the OpenID URL
          String url = manager.getAuthenticationUrl("facebook", successUrl);

          // Store in session
          HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession ses = request.getSession(true);

          ses.setAttribute("authManager", manager);
          System.out.println(url);
          FacesContext.getCurrentInstance().responseComplete();
       FacesContext.getCurrentInstance().getExternalContext().redirect(url);

        }

then finally i redirect to the respective URL given by the facebook, after user logs in into the facebook, then it automatically moves to the succesURL which is was mentioned in the above code.

In my successURL i just have an outputtext.

                          <tr><td class="profile-head"><h:outputText id="name" value="#{employeeProfile.profileName}" /></td></tr>
            <tr><td class="content-black">
            <div class="padding-2"><h:outputText id="name" value="#{employeeProfile.profileName}" />

In the backing bean, i created a session to get the attributes which i set earlier.

                  public class EmployeeProfile {

public String profileName;

public String getProfileName() throws Exception  {

HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    HttpSession ses = request.getSession(true);
    SocialAuthManager m = (SocialAuthManager)ses.getAttribute("authManager");        
    AuthProvider provider = m.connect(SocialAuthUtil.getRequestParametersMap(request));
    Profile p = provider.getUserProfile();
      String userName=p.getFirstName();
      System.out.println(userName);

    return userName;

}

public void setProfileName(String profileName) {
    this.profileName = profileName;
}

when i printed the username in console it does, but its not the view page of this backing bean, have caught up with two exceptions as below.

             1.  javax.faces.FacesException: Could not retrieve value of component with path : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /cv/employee-profile.xhtml][Class: javax.faces.component.html.HtmlOutputText,Id: name]}
           Caused by: javax.el.ELException: /cv/employee-profile.xhtml at line 133 and column 105 value="#{employeeProfile.profileName}": Error reading 'profileName' on type socialServlet.EmployeeProfile       


            2.javax.faces.FacesException: This is not the same SocailAuthManager object that was used for login.Please check if you have called getAuthenticationUrl() method before calling connect()
              Caused by: This is not the same SocailAuthManager object that was used for login.Please check if you have called getAuthenticationUrl() method before calling connect()

Last one is just an inbuilt exception of social auth API, But i dont think any problem in this, because when i try it out with servlet, everything works fine, i think am doing some mistake in JSF session. But i dont know wher i am wrong.

Karthikeyan
  • 757
  • 3
  • 11
  • 25
  • 2
    How many times does it print in the console? 1 or 2 times? You're referencing it 2 times from your view and you're retrieving the data in a getter instead of only once elsewhere (e.g. constructor). If it prints 1 time and then the exception occurs, apparently `m.connect()` should not be invoked 2 times in succession. – BalusC Dec 02 '11 at 13:06
  • you r exactly rite. it prints twice. i have Invoked m.connect twice, now i did everything in the constructor, and its works fine now. – Karthikeyan Dec 03 '11 at 06:13

1 Answers1

0

My problem is now rectified.. the mistake i did is, i have invoked the connect() function again. Now i have did everything in the constructor itself. It works fine.

                public class EmployeeProfile {


public EmployeeProfile() throws Exception {
    // TODO Auto-generated constructor stub




        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest)ectx.getRequest();
        HttpSession session = request.getSession(true);
         SocialAuthManager m = (SocialAuthManager)session.getAttribute("authManager"); 
         AuthProvider provider = m.connect(SocialAuthUtil.getRequestParametersMap(request));
         Profile p = provider.getUserProfile();
             String userName=p.getFirstName();    
              System.out.println(userName);
              setProfileName(userName);
              setBirthDate(p.getDob());
              setImageUrl(p.getProfileImageURL());
              p.getGender();


}
Karthikeyan
  • 757
  • 3
  • 11
  • 25
  • Hi Karthikeyan, thanks for sharing solution to this. I am working on the same and stuck because of SocialAuthManager becomes NULL after redirection. The same is explained at: http://stackoverflow.com/questions/7043854/socialauthmanager-object-manager-becomes-null-after-redirection-in-case-of-s If you know how to fix this I will be verymuch thankfull to you. – Abhishek Dhote Jan 10 '12 at 06:22
  • @Abhishek, I'm stuck with this very problem. Did you figure it out how to solve it ? – Valter Silva Feb 28 '13 at 04:19
  • @Karthikeyan, could you please be more specific in your answer ? It seems the same code, I tested and the problem persist. – Valter Silva Feb 28 '13 at 04:20
  • wat kind of problem you have? – Karthikeyan Feb 28 '13 at 11:14
  • @Karthikeyan the problem was related to some dependent libraries. I had fixed it need to get you how, will respond back. – Abhishek Dhote Nov 26 '13 at 03:52
  • Here is the probable solution to the problem: http://stackoverflow.com/questions/15125452/how-use-socialauth-with-jsf-to-redirect – Abhishek Dhote Nov 27 '13 at 08:35