• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Facebook Login
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Facebook Login


  • Subject: Re: Facebook Login
  • From: Miguel Torres <email@hidden>
  • Date: Thu, 05 Sep 2013 10:09:23 -0500

Thank you very much Amedeo.

We will try it.

Best Regards.


On 05/09/2013, at 04:24, Amedeo Mantica <email@hidden> wrote:

So, here how you get a FaceBook login for your WO Apps

example: http://www.digitmovies.com/Apps/WebObjects/digitmovies.woa/wa/customerLoginPage

Amedeo


1) Login on FaceBook, and create a FaceBook application
or better create two facebook Application, one for development and one for production

<Pasted_Image_05_09_13_11_07.jpg>


<Pasted_Image_05_09_13_11_08.jpg>


2) read this

https://developers.facebook.com/docs/reference/api/field_expansion/

3) Code

3.1)

This is the sction for the WOHyperLink that redirects the user to the facebook login

public WOActionResults facebookLogin() {
ERXRedirect redirect = new ERXRedirect(context());
String url = ""https://www.facebook.com/dialog/oauth?client_id=">https://www.facebook.com/dialog/oauth?client_id=" + System.getProperty("facebookAppId") + "&redirect_uri=" + System.getProperty("facebookReturnUrl") + context().directActionURLForActionNamed("fbLogin", null) + "&scope=email";
redirect.setUrl(url);
return redirect;
}

NOTE: the directaction fbLogin code is balow
System.getProperty("facebookAppId") is your appId
System.getProperty("facebookReturnUrl") is the return url, for me (in development): facebookReturnUrl=http://amedeo.lan.insigno.it:18965


when the user clicks here, if is the First time he will be asked to approve, otherwise will get automatically logged in


3.2)


3.2.1) DirectAction

/** DirectAction callback from Facebook provider */
public WOActionResults fbLoginAction() {
return ((Session) session()).getUserController().executeFaceBookAuthentication(request());
}


3.2.2) UserController

put executeFaceBookAuthentication() whatever you want, I have mine in the UserController class



public WOActionResults executeFaceBookAuthentication(WORequest request) {
try {
String authCode = (String)request.formValueForKey("code");
if ( this._authenticateUser( authCode , request.context() ) ) {
if ( this.isCustomerAuthenticated() ) {
return WOApplication.application().pageWithName("DMCustomerLandingPage", request.context());
}
return WOApplication.application().pageWithName("DMCustomerSubscriptionPage", request.context());
}

} catch (Exception e) {
//
}
return WOApplication.application().pageWithName("DMCustomerLoginPage", request.context());
}
/* Authentication by Facebook*/
private Boolean _authenticateUser(String authCode, WOContext context) {
log.info("authCode: " + authCode);
String requestUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + System.getProperty("facebookAppId") + "&redirect_uri=" + System.getProperty("facebookReturnUrl") + context.directActionURLForActionNamed("fbLogin", null) + "&client_secret=" + System.getProperty("faceBookAppSecret") + "&code=" + authCode;
String token = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(requestUrl);
try {
HttpResponse authResponse = httpClient.execute(httpget);
//log.info("authResponse: " + authResponse.getStatusLine());
HttpEntity entity = authResponse.getEntity();
if (entity != null) {
    InputStream instream = entity.getContent();
    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
        // do something useful with the response
        //System.out.println("TOKEN: " + reader.readLine());
        token = reader.readLine();
        
    } catch (IOException ex) {

        // In case of an IOException the connection will be released
        // back to the connection manager automatically
    ex.printStackTrace();
        throw ex;

    } catch (RuntimeException ex) {

        // In case of an unexpected exception you may want to abort
        // the HTTP request in order to shut down the underlying
        // connection and release it back to the connection manager.
        httpget.abort();
        throw ex;

    } finally {

        // Closing the input stream will trigger connection release
        instream.close();
    }

    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpClient.getConnectionManager().shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
log.debug("TOKEN: " + token);
__faceBookToken = token;
String jsonOut = null;
if (token!=null) {

httpClient = new DefaultHttpClient();
requestUrl = "https://graph.facebook.com/me?" + token;
httpget = new HttpGet(requestUrl);
try {

HttpResponse authResponse = httpClient.execute(httpget);
HttpEntity entity = authResponse.getEntity();
if (entity != null) {
    InputStream instream = entity.getContent();
    
    int length = new Long(entity.getContentLength()).intValue();
    
    byte[] b = new byte[length];
    instream.read(b);
    new String(b);
    jsonOut = new String(b);
    log.debug(jsonOut);
}
} catch (Exception e) {
e.printStackTrace();
}
}
String email = null;
String firstName = null;
String lastName = null;
if (jsonOut!=null) {
log.debug("L: " + jsonOut.length());
JSONParser parser = new JSONParser();
try {
org.json.simple.JSONObject obj = (org.json.simple.JSONObject) parser.parse(jsonOut);
email = (String) obj.get("email");
firstName = (String) obj.get("first_name");
lastName = (String) obj.get("last_name");
} catch (ParseException e) {
System.out.println("position: " + e.getPosition());
   System.out.println(e);
} catch (Exception e) {
e.printStackTrace();
}
}

/* THE CODE BELOW HERE DEPENDS ON YOUR USERS DATABASE  */
try {
DMCustomer customer = (DMCustomer) EOUtilities.objectMatchingKeyAndValue(_session.defaultEditingContext(), DMCustomer.ENTITY_NAME, DMCustomer.EMAIL_KEY, email);
_authenticatedCustomer = customer;
_authenticatedCustomer.setAuthMethod(AuthMethod.FACEBOOK);
return Boolean.TRUE;
} catch (EOObjectNotAvailableException e) {
DMCustomer newCustomer = (DMCustomer) EOUtilities.createAndInsertInstance(_session.defaultEditingContext(), DMCustomer.ENTITY_NAME);
newCustomer.setEmail(email);
newCustomer.setName(firstName);
newCustomer.setSurname(lastName);
_session.defaultEditingContext().saveChanges();
_authenticatedCustomer = newCustomer;
_authenticatedCustomer.setAuthMethod(AuthMethod.FACEBOOK);
return Boolean.TRUE;
} catch (EOUtilities.MoreThanOneException e) {
log.error("Duplicate entry in customers database!");
e.printStackTrace();
_authenticatedCustomer = null;
return Boolean.FALSE;
}
}



Best
Amedeo

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Facebook Login
      • From: Jesse Tayler <email@hidden>
References: 
 >Re: Facebook Login (From: Amedeo Mantica <email@hidden>)

  • Prev by Date: Re: Problem with Deployment in CentOS - multiple app servers -
  • Next by Date: Re: Facebook Login
  • Previous by thread: Re: Facebook Login
  • Next by thread: Re: Facebook Login
  • Index(es):
    • Date
    • Thread