Quantcast
Viewing all articles
Browse latest Browse all 9

Facebook OAUTH

Java OAUTH for FaceBook

Step 1) Register an “app” on Facebook (cf. https://developers.facebook.com/ ). You give Facebook a name for the app and a url. The url you register is the url to the page (jsp or servlet) that you want to handle the login. From the registration you get two strings, an “app ID” and an “app secret” (the latter being your password, do not give this out or write it in html).

Register your website URL (this is the Base for the Return URL where facebook  must redirect after authentication is complete) under this heading “Website with Facebook Login”

For this example, let’s say the url I register is “http://myappengineappid.appspot.com/signin_fb.do”.

2) From a webpage, say with a button, you redirect the user to the following url on Facebook, substituting your app id for “myfacebookappid” in the below example. You also have to choose which permissions (or “scopes”) you want the ask the user (cf. https://developers.facebook.com/docs/reference/api/permissions/ ). In the example I ask for access to the user’s email only.

——————–

1. On first request, the plugin will return NOT_COMPLETE and redirect the user to the Facebook Oauth authentication URL:

https://www.facebook.com/dialog/oauth?client_id={app-id}&redirect_uri={redirect-uri}&scope=email

Where:

{redirect-uri} will be a URL in the form https:///Login/OauthAuthCallback.do. This will either be constructed from the current request URL, or from the requestUri parameter

{app-id} will be the application ID from the parameters

2. On receiving control again, the plugin will inspect the received query parameters. If the error_reason parameter is present, return state COMPLETE and set the oauthResult output field to “failed”. Otherwise:

a) Retrieve the access code using URL: https://graph.facebook.com/oauth/access_token?client_id={app-id}&redirect_uri={redirect-uri}&client_secret={app-secret}&code={auth-code}

Where:

{app-secret} is the application secret from the parameters

{auth-code} is the authentication code received in the request

b) On receiving the response, return output field oauthResult=”failed” if unsuccessful. Otherwise request user information by issueing a GET for the following URL:

https://graph.facebook.com/me?&access_token={access-token}

Where:

{access-token} is the token received in the response to step a).

c) If field “id” is not present in the response, return output field oauthResult=”failed”. Otherwise, populate the following output fields:

facebook_id (from “id” element)

facebook_firstname (from “first_name” element)

facebook_lastname (from “last_name” element)

facebook_email (from “email” element)

d) Request the user’s profile picture by issueing a GET for the following URL:

https://graph.facebook.com/me?fields=picture&access_token={access-token}

This returns an image URL, add it to the output fields as facebook_picture_url.

e) Return state COMPLETE with ouathResult=”success”

  1.   private String getUserData(String accessToken, String uri) throws Exception {
  2.  
  3.     String responseData = "";
  4.  
  5.     URL url = new URL(uri);
  6.  
  7.     HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  8.  
  9.     connection.setDoOutput(true);
  10.  
  11.     connection.setRequestMethod(GET);
  12.  
  13.     connection.addRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
  14.  
  15.     connection.addRequestProperty(AUTHORIZATION, "OAuth " + accessToken);
  16.  
  17.     BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  18.  
  19.     String input;
  20.  
  21.     while ((input = br.readLine()) != null) {
  22.  
  23.       OauthLogger.logDebug(input);
  24.  
  25.       responseData += input;
  26.  
  27.     }
  28.  
  29.     br.close();
  30.  
  31.     return responseData;
  32.  
  33.   }
  34.  
  35.   private String getAccessToken(String authorizeUrl) throws Exception {
  36.  
  37.     URL url = new URL(authorizeUrl);
  38.  
  39.     HttpsURLConnection connection1 = (HttpsURLConnection) url.openConnection();
  40.  
  41.     BufferedReader br = new BufferedReader(new InputStreamReader(connection1.getInputStream()));
  42.  
  43.     String accessToken = null;
  44.  
  45.     String input;
  46.  
  47.     while ((input = br.readLine()) != null) {
  48.  
  49.       OauthLogger.logDebug(input);
  50.  
  51.       if (input.contains("access_token=")) {
  52.  
  53.         accessToken = input.substring(input.indexOf("=") + 1, input.length());
  54.  
  55.       }
  56.  
  57.     }
  58.  
  59.     br.close();
  60.  
  61.     return accessToken;
  62.  
  63.   }
  64.  
  65.   private String getUserProfileImage(String graphgUri) throws Exception {
  66.  
  67.     URLConnection connection = null;
  68.  
  69.     StringBuilder sb = new StringBuilder();
  70.  
  71.     connection = new URL(graphgUri).openConnection();
  72.  
  73.     connection.addRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
  74.  
  75.     BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  76.  
  77.     String inputLine;
  78.  
  79.     while ((inputLine = br.readLine()) != null) {
  80.  
  81.       sb.append(inputLine);
  82.  
  83.     }
  84.  
  85.     br.close();
  86.  
  87.     return sb.toString();
  88.  
  89.   }

 


Viewing all articles
Browse latest Browse all 9

Trending Articles