<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jozef Chúťka&#039;s blog &#187; FacebookLogger</title>
	<atom:link href="http://blog.yoz.sk/tag/facebooklogger/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yoz.sk</link>
	<description>My life, my work</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Elegant Facebook Login For Desktop Application</title>
		<link>http://blog.yoz.sk/2010/01/elegant-facebook-login-for-desktop-application/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=elegant-facebook-login-for-desktop-application</link>
		<comments>http://blog.yoz.sk/2010/01/elegant-facebook-login-for-desktop-application/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 11:24:29 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[Authorization]]></category>
		<category><![CDATA[DesktopSession]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[FacebookLogger]]></category>
		<category><![CDATA[FacebookPopup]]></category>
		<category><![CDATA[FlexIFrame]]></category>
		<category><![CDATA[IFrame]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=915</guid>
		<description><![CDATA[Default facebook login page is usualy opened in new browser window or popup window, so user lose focus from your page. This application opens facebook login page within your flash application, so user will not lose your page. In fact little iframe trick is used. This post connects my 3 previous posts: Flex IFrame – [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/elegantFacebookLogin.jpg" alt="" title="elegantFacebookLogin" width="200" height="124" class="alignleft size-full wp-image-920" /></p>
<p>Default facebook login page is usualy opened in new browser window or popup window, so user lose focus from your page. This application opens facebook login page within your flash application, so user will not lose your page. In fact little iframe trick is used.</p>
<p>This post connects my 3 previous posts: <a href="http://blog.yoz.sk/2009/10/flex-iframe-web-browser-in-flash/">Flex IFrame – Web browser in flash</a>, <a href="http://blog.yoz.sk/2009/12/facebooklogger/">FacebookLogger</a> and <a href="http://blog.yoz.sk/2010/01/facebook-extended-permissions-with-authorization-by-overriding-class-in-swc/">Facebook Extended Permissions With Authorization by Overriding Class in swc</a> in order to create elegant solution for facebook connect with your desktop application (means outside facebook iframe or fbml). I recomend to read all previous mentioned posts before trying to run this app.</p>
<p><span id="more-915"></span></p>
<p>Final application (view source enabled)</p>
<p><iframe src="http://blog.yoz.sk/examples/elegantFacebookLogin/" style="border:none;" width="100%" height="600"></iframe></p>
<p>FB class. Read more about <a href="http://blog.yoz.sk/2009/12/facebooklogger/">FacebookLogger</a> class here.</p>
<pre class="brush: as3; title: ; notranslate">package
{
    import flash.net.URLRequest;
    import flash.net.URLVariables;

    import mx.core.Application;

    import sk.yoz.events.FacebookLoggerEvent;
    import sk.yoz.net.FacebookLogger;

    public class FB extends FacebookLogger
    {
        private static const SINGLETON_LOCK:Object = {};
        private static const _instence:FB = new FB(SINGLETON_LOCK);

        protected var API_KEY:String = &quot;bc55511efba53af7a3529ea9e7d1989b&quot;;
        protected var APP_SECRET:String = &quot;727616ab7a9acb969e7cec3c7de04d79&quot;;

        private var initCallback:Function;

        public function FB(lock:Object)
        {
            super();

            if(lock != SINGLETON_LOCK)
                throw new Error(&quot;Use FB.instance!&quot;);

            addEventListener(FacebookLoggerEvent.CONNECTED, loggerConnectedHandler)
        }

        public static function get instance():FB
        {
            return _instence;
        }

        public function init2(application:Application):void
        {
            if(public::connected)
                return;
            init(API_KEY, APP_SECRET, application.loaderInfo);
        }

        public function initWithCallback(application:Application, callback:Function):void
        {
            initCallback = callback;
            init2(application);
        }

        private function loggerConnectedHandler(event:FacebookLoggerEvent):void
        {
            if(initCallback != null)
                initCallback();
            initCallback = null;
            // now we are connected, your code may go here
        }

        public function openPopup(login_url:String, auth_token:String):void
        {
            var request:URLRequest = new URLRequest(login_url);
            var variables:URLVariables = new URLVariables();

            variables.req_perms = 'publish_stream';
            variables.api_key = facebook.api_key;
            variables.v = facebook.api_version;
            variables.auth_token = auth_token;
            variables.fbconnect = &quot;true&quot;;
            variables.connect_display = &quot;popup&quot;;

            request.data = variables;

            FacebookPopup.open(request);
        }
    }
}</pre>
<p>DesktopSession class. Read more about <a href="http://blog.yoz.sk/2010/01/facebook-extended-permissions-with-authorization-by-overriding-class-in-swc/">Overriding Class in swc</a>.</p>
<pre class="brush: as3; title: ; notranslate">... some code ...

protected function onLogin(p_event:FacebookEvent):void {
	p_event.target.removeEventListener(FacebookEvent.COMPLETE, onLogin);

	if (p_event.success) {
		_auth_token = (p_event.data as StringResultData).value;

		FB.instance.openPopup(login_url, _auth_token);

		_waiting_for_login = true;
		dispatchEvent(new FacebookEvent(FacebookEvent.WAITING_FOR_LOGIN));
	} else {
		onConnectionError(p_event.error);
	}
}

... some code ...</pre>
<p>FacebookPopup class. Read more about <a href="http://blog.yoz.sk/2009/10/flex-iframe-web-browser-in-flash/">Flex IFrame</a>.</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:TitleWindow xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;vertical&quot;
    width=&quot;510&quot; height=&quot;465&quot; xmlns:html=&quot;sk.yoz.html.*&quot; move=&quot;moveHandler()&quot;
    title=&quot;Facebook login&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    import mx.core.Application;
    import mx.managers.PopUpManager;

    [Bindable]
    protected var url:String;

    [Bindable]
    protected var fb:FB = FB.instance;

    protected function moveHandler():void
    {
        iframe.positionChanged = true;
        iframe.invalidateProperties();
    }

    public function set request(value:URLRequest):void
    {
        var url:String = value.url;
        url += &quot;?&quot;;
        for(var param:String in value.data)
            url += &quot;&amp;&quot; + param + &quot;=&quot; + value.data[param];
        this.url = url;
    }

    public static function open(request:URLRequest):void
    {
        var window:FacebookPopup = new FacebookPopup();
        window.request = request;
        window.visible = true;
        PopUpManager.addPopUp(window, Application(Application.application));
        PopUpManager.centerPopUp(window);
    }

    protected function validateLogin():void
    {
        fb.validate();
        close();
    }

    protected function close():void
    {
        visible = false;
        PopUpManager.removePopUp(this);
    }

    protected function refresh():void
    {
        iframe.url = url;
    }
]]&gt;
&lt;/mx:Script&gt;
&lt;html:IFrame width=&quot;100%&quot; height=&quot;100%&quot; id=&quot;iframe&quot; autoResize=&quot;true&quot;
    url=&quot;{url}&quot; visible=&quot;{visible}&quot;/&gt;
&lt;mx:HBox width=&quot;100%&quot;&gt;
    &lt;mx:Text text=&quot;Clik Ok after you login to Facebook.&quot; /&gt;
    &lt;mx:Spacer width=&quot;100%&quot; /&gt;
    &lt;mx:Label text=&quot;refresh&quot; click=&quot;refresh()&quot; textDecoration=&quot;underline&quot;
        buttonMode=&quot;true&quot;/&gt;
&lt;/mx:HBox&gt;
&lt;mx:HBox width=&quot;100%&quot; horizontalAlign=&quot;center&quot; &gt;
    &lt;mx:Button label=&quot;Ok&quot; click=&quot;validateLogin()&quot;/&gt;
    &lt;mx:Button label=&quot;Cancel&quot; click=&quot;close()&quot;/&gt;
&lt;/mx:HBox&gt;
&lt;/mx:TitleWindow&gt;</pre>
<p>Application (do not forget to add facebook flex .swc, version 3.4 used in example)</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    import FB;

    [Bindable]
    private var fb:FB = FB.instance;
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:Button label=&quot;Facebook connect&quot; click=&quot;fb.init2(this)&quot;/&gt;
&lt;mx:Text text=&quot;{fb.connected ? 'connected' : 'not connected'}&quot; /&gt;
&lt;/mx:Application&gt;</pre>
<p>Application html template. <a href="http://blog.yoz.sk/2009/10/flex-iframe-web-browser-in-flash/">More about flexiframe.js here</a>.</p>
<pre class="brush: xml; title: ; notranslate">... some code ...
&lt;script type=&quot;text/javascript&quot; src=&quot;flexiframe.js&quot;&gt;&lt;/script&gt;
... some code ...</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/01/elegant-facebook-login-for-desktop-application/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>FacebookLogger</title>
		<link>http://blog.yoz.sk/2009/12/facebooklogger/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=facebooklogger</link>
		<comments>http://blog.yoz.sk/2009/12/facebooklogger/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 15:22:34 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[FacebookEvent]]></category>
		<category><![CDATA[FacebookLogger]]></category>
		<category><![CDATA[FacebookLoggerEvent]]></category>
		<category><![CDATA[FacebookSessionUtil]]></category>
		<category><![CDATA[LoaderInfo]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=750</guid>
		<description><![CDATA[In a last couple of weeks I have been working on a lot of facebook applications. It did not took long to realize that there is always the same logic behind logging/connecting to facebook. So I came to FacebookLogger abstraction class that handles these basic steps. It only contains login/connection logic, so its pretty much [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.yoz.sk/wp-content/uploads/facebook-small-logo1.jpg"><img src="http://blog.yoz.sk/wp-content/uploads/facebook-small-logo1.jpg" alt="facebook-small-logo" title="facebook-small-logo" width="200" height="134" class="alignleft size-full wp-image-763" /></a></p>
<p>In a last couple of weeks I have been working on a lot of facebook applications. It did not took long to realize that there is always the same logic behind logging/connecting to facebook. So I came to FacebookLogger abstraction class that handles these basic steps. It only contains login/connection logic, so its pretty much the core that is usable only after extending.</p>
<p><span id="more-750"></span></p>
<p style="clear:both;"><a href="http://classes.yoz.sk/sk/yoz/net/FacebookLogger.as">FacebookLogger</a> class look like this:</p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.net
{
    import com.facebook.Facebook;
    import com.facebook.events.FacebookEvent;
    import com.facebook.utils.FacebookSessionUtil;

    import flash.display.LoaderInfo;
    import flash.events.EventDispatcher;

    import sk.yoz.events.FacebookLoggerEvent;

    public class FacebookLogger extends EventDispatcher
    {
        protected var facebook:Facebook;
        protected var session:FacebookSessionUtil;

        private var _connected:Boolean = false;

        public function FacebookLogger()
        {
            super();
        }

        [Bindable(event=&quot;FacebookLoggerEventCONNECTED&quot;)]
        public function get connected():Boolean
        {
            return _connected;
        }

        protected function set connected(value:Boolean):void
        {
            _connected = value;
            var type:String = FacebookLoggerEvent.CONNECTED;
            dispatchEvent(new FacebookLoggerEvent(type));
        }

        public function init(apiKey:String, appSecret:String,
            loaderInfo:LoaderInfo):void
        {
            session = new FacebookSessionUtil(apiKey, appSecret, loaderInfo);
            session.addEventListener(FacebookEvent.CONNECT, connectHandler);
            facebook = session.facebook;

            if(loaderInfo.parameters.fb_sig_session_key)
                 session.verifySession();
            else
                login();
        }

        protected function login():void
        {
            session.login();
        }

        public function validate():void
        {
            session.validateLogin();
        }

        protected function connectHandler(event:FacebookEvent):void
        {
            if(!event.success)
                return login();

            protected::connected = true;
        }
    }
}</pre>
<p>&#8230; and you also gonna require <a href="http://classes.yoz.sk/sk/yoz/events/FacebookLoggerEvent.as">FacebookLoggerEvent</a> class &#8230;</p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.events
{
    import flash.events.Event;

    public class FacebookLoggerEvent extends Event
    {
        public static const CONNECTED:String = &quot;FacebookLoggerEventCONNECTED&quot;;

        public function FacebookLoggerEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }

    }
}</pre>
<p>Now, lets extend the core class. This class should hold all the required logic and communication with facebook, its fine to make it singleton:</p>
<pre class="brush: as3; title: ; notranslate">package
{
    import mx.controls.Alert;
    import mx.core.Application;
    import mx.events.CloseEvent;

    import sk.yoz.events.FacebookLoggerEvent;
    import sk.yoz.net.FacebookLogger;

    public class FB extends FacebookLogger
    {
        public static const NOT_LOGGED:String = &quot;Seems like not logged, click when logged!&quot;;

        private static const SINGLETON_LOCK:Object = {};
        private static const _instence:FB = new FB(SINGLETON_LOCK);

        protected var API_KEY:String = &quot;***&quot;;
        protected var APP_SECRET:String = &quot;***&quot;;

		private var initCallback:Function;

        public function FB(lock:Object)
        {
            super();

            if(lock != SINGLETON_LOCK)
                throw new Error(&quot;Use FB.instance!&quot;);

            addEventListener(FacebookLoggerEvent.CONNECTED, loggerConnectedHandler)
        }

        public static function get instance():FB
        {
            return _instence;
        }

        public function init2(application:Application):void
        {
			if(public::connected)
                return;
            init(API_KEY, APP_SECRET, application.loaderInfo, application.parameters);
        }

        public function initWithCallback(application:Application, callback:Function):void
        {
            initCallback = callback;
            init2(application);
        }

        override protected function login():void
        {
            super.login();
            Alert.show(NOT_LOGGED, null, 4, null, loggedClose);
        }

        protected function loggedClose(event:CloseEvent):void
        {
            validate();
        }

        private function loggerConnectedHandler(event:FacebookLoggerEvent):void
        {
			if(initCallback != null)
                initCallback();
			initCallback = null;
            // now we are connected, your code may go here
        }
    }
}</pre>
<p>Update (Jan 6, 2009): Suggested initWithCallback() function where you can define callback function that is called after successful connect.</p>
<p>&#8230; so we can use in application as simple as possible:</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;absolute&quot;
    addedToStage=&quot;addedToStageHandler()&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    import FB;

    [Bindable]
    private var fb:FB = FB.instance;

    private function addedToStageHandler():void
    {
        fb.init2(this);
    }
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:Text text=&quot;{fb.connected ? 'connected' : 'connecting'}&quot; /&gt;
&lt;/mx:Application&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/12/facebooklogger/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

