Monday, November 5, 2012

How to authenticate with Facebook and write info into ACS

After searching, asking and many trial and error attempts I found a way to authenticate users using Facebook, and then entering their FB user data (email address and name) into the Appcelerator's Cloud Service.

Why am I posting it here in questions? 1) to see if there is a better way, streamline it and get even more input from the community and 2) share what is often looked for but not that clear or combined into one code example.

If you have any input, suggestions, etc. please add. Hope this helps some but also interested to see what others have to add.

// javascript in this page is called by an existing window so you don't see the window being created. // adding notes like this, I hope to help the very new to javascript or even Titanium // I know most of the comments are overkill but hope some find it handy // var Cloud = require('ti.cloud'); // make sure cloud is active in your tiapp.xml too Titanium.Facebook.appid = "xxxxxxxxxxxxxxxx"; // use your Facebook app id Titanium.Facebook.permissions = ['publish_stream', 'read_stream', 'email']; // set the permissions you want from FB ? //Titanium.Facebook.logout(); // can use for testing ? // Calls the FB authentication process without a button required Titanium.Facebook.authorize(); ? // Listen to get token back from FB Titanium.Facebook.addEventListener('login', function(e) {     if (e.success) {         Cloud.SocialIntegrations.externalAccountLogin({             type : 'facebook',             token : Ti.Facebook.accessToken         }, function(e) {             if (e.success) { // If successful, get more user details and write to console for testing Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', function(e) {                 if (e.success) {                    var data= JSON.parse(e.result);                     Ti.API.info("Name:"+data.name);                     Ti.API.info("email:"+data.email);                     Ti.API.info("facebook Id:"+data.id);  // Add user info to array for input into ACS                     var data = {             username: data.email,             email: data.email         }  // Write info to ACS and include error checking, etc.               Ti.API.info("Data:" +data);             Cloud.Users.update(data, function (e) {             if (e.success) {                 // If successful, write to console for testing...                 Ti.API.info("updated data");                 // ...and include page for registered users to see.                 Ti.include("accountRegistered.js");             }             });             // Error checking alerts.                 } else if (e.error) {                     alert(e.error);                 } else {                     alert('Unknown response');                 }             }); // More error checking prior to ending functions.             } else {                 alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));             }         });     } else if (e.error) {         alert("Error = " + e.error);     } else if (e.cancelled) {         alert("Unknown error with FB login.");     } });

Source: http://developer.appcelerator.com/question/144332/how-to-authenticate-with-facebook-and-write-info-into-acs

current tv megamillions ncaa basketball tournament 2012 megamillions winning numbers lotto winner michael oher jerry lee lewis

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.