Monday, 26 November 2012

Facebook Login using Graph API and OAuth 2.0


This script
- uses Open Graph Apis in Java servlets to authenticate users on your website.
- pulls the user details from its Facebook account using RestFb library and displays the same on your webpage.
- has Fb logout facility.





Download Pack     Live Demo    


How to use:

The download pack contains the complete project structure with the sample war file that I have deployed in my live demo.

1. Create a Facebook Application

You need to create a Facebook app and specify your website that requires a Facebook login.
Also note the App Id and App Secret for your Fb app.



2. Web.xml (Input variables)

Specify the AppId and AppSecret from your Facebook app in your web.xml
Also the additional permissions are mentioned here. You may update these permissions as required.
After successful login welcome.jsp will be shown to the user.



3. Welcome.jsp (Output variables)

The user account details will be available as session attributes in welcome.jsp
The object fbUser contains these attributes as shown. You may write your own display code instead, as required.


Let me know if you face any issues.

9 comments:

  1. Great work.. Thanks. perfectly working your Sample application

    ReplyDelete
  2. type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: java.lang.NullPointerException
    com.fblogin.CallbackServlet.doGet(CallbackServlet.java:100)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    com.gylogin.EncodingFilter.doFilter(EncodingFilter.java:24)

    root cause

    java.lang.NullPointerException
    com.fblogin.CallbackServlet.doGet(CallbackServlet.java:86)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    com.gylogin.EncodingFilter.doFilter(EncodingFilter.java:24)

    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

    ReplyDelete
  3. Hi, I used your code and followed your instructions but i have an error:
    {
    "error": {
    "message": "Invalid redirect_uri: Given URL is not allowed by the Application configuration.",
    "type": "OAuthException",
    "code": 191
    }
    }

    what should i do about that?
    PLease help thanks.

    ReplyDelete
    Replies
    1. Please make sure that 'website url' in your Facebook App Settings is correctly.

      Delete
  4. will localhost also work as domain in your example ?

    ReplyDelete
    Replies
    1. It works perfectly, as far as I know.Just mention the port in case you use one.
      eg : http://localhost:8080/
      Please mention this url as redirect_url in your app settings on facebook.

      Delete
  5. Hi my friend,App_id and app_secret have changed but didn't it working.What should i do

    ReplyDelete
  6. it can't work on localhost, you have to upload the code to a host server

    ReplyDelete
  7. Hi,

    I am getting the null pointer exception as follows

    SEVERE: Servlet.service() for servlet [callback] in context with path [/FBLOGINAPP] threw exception [java.lang.NullPointerException] with root cause
    java.lang.NullPointerException

    Can Any one guide me to get rid of this?

    ReplyDelete