or better create two facebook Application, one for development and one for production
This is the sction for the WOHyperLink that redirects the user to the facebook login
when the user clicks here, if is the First time he will be asked to approve, otherwise will get automatically logged in
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();
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;
}
}