0

I'm following the steps that are mentioned in this answer: https://stackoverflow.com/a/9580644/4950201

I succeeded in registering a Facebook app and invoking the login dialog from a webpage (The user clicks login to Facebook, he logs in, and Facebook redirect him to the redirect_uri with a code appended to it).

Now I have no clue on how to use the following Java code on the server side (Taken from the answer above):

import org.json.JSONObject;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SignInFB extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {            
    String code = req.getParameter("code");
    if (code == null || code.equals("")) {
        // an error occurred, handle this
    }

    String token = null;
    try {
        String g = "https://graph.facebook.com/oauth/access_token?client_id=myfacebookappid&redirect_uri=" + URLEncoder.encode("http://myappengineappid.appspot.com/signin_fb.do", "UTF-8") + "&client_secret=myfacebookappsecret&code=" + code;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");            
        in.close();
        token = b.toString();
        if (token.startsWith("{"))
            throw new Exception("error on requesting token: " + token + " with code: " + code);
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String graph = null;
    try {
        String g = "https://graph.facebook.com/me?" + token;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");            
        in.close();
        graph = b.toString();
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String facebookId;
    String firstName;
    String middleNames;
    String lastName;
    String email;
    Gender gender;
    try {
        JSONObject json = new JSONObject(graph);
        facebookId = json.getString("id");
        firstName = json.getString("first_name");
        if (json.has("middle_name"))
           middleNames = json.getString("middle_name");
        else
            middleNames = null;
        if (middleNames != null && middleNames.equals(""))
            middleNames = null;
        lastName = json.getString("last_name");
        email = json.getString("email");
        if (json.has("gender")) {
            String g = json.getString("gender");
            if (g.equalsIgnoreCase("female"))
                gender = Gender.FEMALE;
            else if (g.equalsIgnoreCase("male"))
                gender = Gender.MALE;
            else
                gender = Gender.UNKNOWN;
        } else {
            gender = Gender.UNKNOWN;
        }
    } catch (JSONException e) {
        // an error occurred, handle this
    }

    ...

When the user logs in, Facebook redirect him to the redirect_uri, and at the redirect_uri page I have to call the server side function service from the client side (from a script to call @service)?

If yes, then what parameters should I pass to the service function? (it gets the following parameters (HttpServletRequest req, HttpServletResponse res) )

I understand the Java code and the process to get the access token, however I don't have any clue in how to use and call the function above after Facebook redirects the user to the redirect_uri.

I'll be happy if someone can help me.

Thanks

Community
  • 1
  • 1
02416
  • 1

1 Answers1

0

You do not have to call this from a script. Basically, your redirect URI itself should be a @RequestMapping. Within this request mapping you would have to write logic to capture the access_token provided to you by Facebook (the code that you see appended to the mentioned redirect URI) and store it securely.

All further interactions with FB should be written as separate @RequestMapping within the controller. For e.g. you could have a @RequestMapping say ListFriends where you would list all your friends.

The logic within this ListFriends servlet would communicate with the Facebook API using the access token (obtained in the previous step) and then return the list to the layer that would render the list to a web page.

Hope this helps.

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • The servlet is basically a java file right? I'm asking because my project is in a model-controller-view architecture, and every page is a regular page in html and in the page I make the call to the server. So how the uri itself can be a servlet in this case? Thanks :-) – 02416 May 28 '15 at 17:56
  • And is there any problem testing this on a localhost link? – 02416 May 28 '15 at 18:10
  • @02416 I did not know at the beginning that you are using MVC. Updated my answer assuming you are using the Spring MVC framework – Prahalad Deshpande May 28 '15 at 18:27
  • @02416 I do not think there should be any problem using the localhost link. But am not too sure. Normally I would host my app on Heroku or some other hosting service and provide that URI. – Prahalad Deshpande May 28 '15 at 18:29
  • Ok thank You. Regarding that the uri itself should be @RequestMapping , this wouldn't cause error because in the application setting on Facebook there is a regular link? (http://...) – 02416 May 28 '15 at 18:37
  • @02416 No that wont be an error. That redirect URI is a URI specified by you and hence should not be a problem. – Prahalad Deshpande May 28 '15 at 18:41