<?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; fb_sig_session_key</title>
	<atom:link href="http://blog.yoz.sk/tag/fb_sig_session_key/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>Update: Facebook application missing session key on first visit</title>
		<link>http://blog.yoz.sk/2009/11/facebook-application-missing-session-key-on-first-visit/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=facebook-application-missing-session-key-on-first-visit</link>
		<comments>http://blog.yoz.sk/2009/11/facebook-application-missing-session-key-on-first-visit/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 11:39:40 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Authorize]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[FBML]]></category>
		<category><![CDATA[fb_sig_session_key]]></category>
		<category><![CDATA[Redirect]]></category>
		<category><![CDATA[verifySession]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=691</guid>
		<description><![CDATA[There are still some issues with facebook flash api vs. facebook communication. Some of them, you as an application developer, will never get into, but general user will. For example, when new user comes into your facebook iframe flash application for a first time, the first thing he see is &#8220;Allow Access?&#8221; window: After clicking [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.yoz.sk/wp-content/uploads/facebookAllowAccess.png"><img src="http://blog.yoz.sk/wp-content/uploads/facebookAllowAccess-200x82.png" alt="facebookAllowAccess" title="facebookAllowAccess" width="200" height="82" class="alignleft size-medium wp-image-692" /></a></p>
<p>There are still some issues with facebook flash api vs. facebook communication. Some of them, you as an application developer, will never get into, but general user will. For example, when new user comes into your facebook iframe flash application for a first time, the first thing he see is &#8220;Allow Access?&#8221; window:</p>
<p><span id="more-691"></span></p>
<div style="clear:both;"></div>
<p><a href="http://blog.yoz.sk/wp-content/uploads/facebookAllowAccess.png"><img src="http://blog.yoz.sk/wp-content/uploads/facebookAllowAccess.png" alt="facebookAllowAccess" title="facebookAllowAccess" width="477" height="197" class="alignnone size-full wp-image-692" /></a></p>
<p>After clicking &#8220;Allow&#8221;, you should serve user your flash app. But, notice there is no fb_sig_session_key parameter in facebook iframe src. So you wount be able to verify user session with:</p>
<pre class="brush: as3; title: ; notranslate">if(parameters.fb_sig_session_key)
    session.verifySession();
else
    session.login()</pre>
<p>&#8230; and new window is opened instead (that handles the session) so users get confused&#8230;</p>
<h3>Solution 1</h3>
<p>After some thinking I came to this idea. Once user allowed your app, it is enough to refresh page and the session parameter is available, this can be done in javascript:</p>
<pre class="brush: jscript; title: ; notranslate">FB.Bootstrap.requireFeatures([&quot;Connect&quot;], function(){
    FB.Facebook.init(api_key, xd_receiver);

    // open facebook &quot;Allow Access&quot; window:
    FB.Connect.requireSession(function() {
        // user clicked on &quot;Allow&quot;
        // ask for fb_sig_session_key parameter if not found
        if(!getRequestParameter(&quot;fb_sig_session_key&quot;)){
            // lets ask for it with simple refresh
            top.location =  &quot;http://apps.facebook.com/MYAPPNAME/&quot;;
        }else{
            // we have fb_sig_session_key, ready for flash app
        }
    });
}

// support function, you can use your own test
function getRequestParameter(name){
    name = name.replace(/[\[]/,&quot;\\\[&quot;).replace(/[\]]/,&quot;\\\]&quot;);
    var regex = new RegExp(&quot;[\\?&amp;]&quot; + name + &quot;=([^&amp;#]*)&quot;);
    var results = regex.exec(window.location.href);
    return results == null ? &quot;&quot; : results[1];
}</pre>
<h3>Solution 2</h3>
<p>Update: I just discovereded, there is a cleaner solution, as the <a href="http://wiki.developers.facebook.com/index.php/Authorizing_Applications">authorizing application spec says</a>. It states there that:</p>
<blockquote><p>Facebook passes the following parameters to your application when a user interacts with your application:<br />
fb_sig_added:  If set to true, then the user has authorized your application.<br />
&#8230;</p></blockquote>
<blockquote><p>Facebook passes the following parameters only if fb_sig_added is true (that is, if the user has authorized your application):<br />
fb_sig_session_key: &#8230;</p></blockquote>
<p>Facebook offers server side or client side login request (read <a href="http://wiki.developers.facebook.com/index.php/Authorizing_Applications">section How Users Can Authorize an Application</a>) in combination with <a href="http://wiki.developers.facebook.com/index.php/Post-Authorize_Redirect_URL">Post-Authorize Redirect URL</a>. Solutions works this way:</p>
<ol>
<li>new user comes to your app</li>
<li>your app (client side or server side) redirects user to original facebook page where user can authorize your application (<a href="http://blog.yoz.sk/wp-content/uploads/authorizeApplicationOnFacebook.png">see image</a>)</li>
<li>after authorization user is redirected to your app again (Post-Authorize Redirect URL used, typically, you want to make this URL your canvas page)</li>
<li>BEHOLD user is back on your app with fb_sig_session_key</li>
</ol>
<p>Here is some code for server side redirect:</p>
<pre class="brush: php; title: ; notranslate">&lt;?php
require_once 'facebook.php';

$appapikey = 'YOUR API KEY';
$appsecret = 'YOUR APPLICATION SECRET';
$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook-&gt;require_login();</pre>
<p>&#8230; or use client side (FBML) code to redirect:</p>
<pre class="brush: xml; title: ; notranslate">&lt;fb:if-is-app-user&gt;
  &lt;!-- your normal code --&gt;
  &lt;fb:else&gt;
   &lt;fb:redirect url=&quot;http://www.facebook.com/login.php?v=1.0&amp;api_key=[your_app_key]&amp;next=[your_canvas_page_URL]&amp;canvas=&quot;/&gt;
  &lt;/fb:else&gt;
&lt;/fb:if-is-app-user&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/11/facebook-application-missing-session-key-on-first-visit/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

