Saturday, 1 December 2012

Login with Instagram Using OAuth 2.0 in Java Servlets

This script
- uses OAuth 2.0 in Java servlets to authenticate users on your website using Instagram.
- pulls the user details from its Instagram account and displays the same on your webpage.
- has 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 an Instagram Application

You need to create a Instagram app and specify your website that requires a Instagram login.
Specify a callback URL that will be a servlet in this case that will receive the auth-token along with user account details.
Also note the Client Id and Client Secret for your app.

2. Web.xml (Input variables)

Specify the Client ID and Client Secret from your Instagram 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 instagramUser contains these attributes as shown. You may write your own display code instead, as required.

Let me know if you face any issues.


  1. How we can authorize an instagram user to my website

    1. Well, first you must follow the tutorial and have an instagram login button on your website.

      Now when any user clicks the login button and logs in with his instagram credentials, he becomes an authorized user for your website. Check out the Live Demo.

      You may store his login id etc. in your database if required for future refernce.

  2. java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
    java.lang.Class.getDeclaredConstructors0(Native Method)
    java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    java.lang.Class.getConstructor0(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process($ Source)

    root cause

    i am getting above error.I tried to run the above code.

    1. You need a Jar file called "httpcore-x.x.x.jar" in your class path. I have included the same in the download .. just make sure its in your WEB-INF/lib folder and in your class path ..

      If it still does not work download the latest version of "httpcore" jar file and include that in your lib folder.

      [Note- org/apache/http/HttpEntity is a class file in the httpcore jar]

  3. Hi I am trying to authorize the user,its returning null pointer exception,i am trying to debug the CallbackServlet its going to httpPost.releaseConnection(),can the callback or website url be "http://localhost:8080/SimpleServlet/Callback.I am not able to check the reason,may i know the best way to find out the solution.


    1. Your callback URL is at localhost:8080 URL, it wont work.
      The application should be hosted externally (eg: and this callback URL in your application should match the redirect_Uri that you enter in your Instagram App on

  4. Thanks a lot,your suggestion worked for me i placed the httpcore jar in the WEB-INF lib it worked for me.Ya it was path issue.
    But now i am getting the nullpointer exception.When i am trying to debug its going to releaseConnection().

    1. 1. Included all the other jars that i have provided (httpclient.x.x.x.jar and json-simple-x.x.x.jar) in your WEB-INF/lib and in your class path

      2. If your callback URL is at localhost:8080 URL, it wont work. The application should be hosted externally (eg: and this callback URL in your application should match the redirect_Uri that you enter in your Instagram App on

  5. Thanks a lot jake,login code using OAuth worked for me.I appreciate your help.
    Finaly i have one doubt,can we open the instagram login page in a iframe.


    1. Buddy, after the user types his credentials and logs in, the Instagram server redirects the user to any page of your choice ... in this case "welcome.jsp" (which you can change) ....

      Now if you open the Login page in an iframe, Instagram will redirect your users and welcome.jsp will be opened in the iframe!!!

      So to answer your question - No it wont be appropriate to open the login page in an iframe (Even if you achieve it by some means it would become a complex redirection system)

      Let me know if you need anything else :)

  6. This comment has been removed by the author.

  7. Thank you very much for the helpful tutorial. I have a question. The user info is got by webContent.get("user"). To get media or comments what should be done? I've tried webContent.get("media") and webContent.get("comments") but no luck..

    1. Only the User object is available, as part of the login functionality..

      You may have to write your code further if you need media associated with the user..