Thank you very much Amedeo. We will try it.
Best Regards. On 05/09/2013, at 04:24, Amedeo Mantica <[email protected]> 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=" > + 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 protected]) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [email protected]
