Search code examples
apache-flexactionscript-3facebook-graph-api

Using the Facebook API after JavaScript Login


This is something I have been trying to figure out, but I am not sure exactly how to do it. I have a flex application that logs into facebook, but after that I can't access any of the facebook api. Right now I am using this HTML to log in:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <!-- Include support librarys first -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>     
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

    <script type="text/javascript">
        //This example uses the javascript sdk to login before embedding the swf

        var APP_ID = "[My App ID Here]";            
        var REDIRECT_URI = "http://apps.facebook.com/isotesthoskins/";      

        var PERMS = "publish_stream,offline_access"; //comma separated list of extended permissions

        function init() {
            FB.init({appId:APP_ID, status: true, cookie: true});
            FB.getLoginStatus(handleLoginStatus);
        }

        function handleLoginStatus(response) {
            if (response.session) { //Show the SWF                                      

                //A 'name' attribute with the same value as the 'id' is REQUIRED for Chrome/Mozilla browsers
                swfobject.embedSWF("isotest.swf", "flashContent", "760", "500", "9.0", null, null, null, {name:"flashContent"});

            } else { //ask the user to login                    

                var params = window.location.toString().slice(window.location.toString().indexOf('?'));                 
                top.location = 'https://graph.facebook.com/oauth/authorize?client_id='+APP_ID+'&scope='+PERMS+'&redirect_uri='+REDIRECT_URI+params;                                     

            }
        }
        $(init);
    </script>

And everything logs in fine, but when I try this in the application after I am logged in, nothing happens.

Facebook.api("/me", function(response){
                            changeText.text = response.name;
                        });

I don't need to init because it was done by the javascript login, right? I might be wrong about that though.


Solution

  • Looks like you are calling the API using the Flex SDK.

    That is not going to work, as the token is not shared between JS and Flex.

    You should login on the Flex side or thunk into the JS to make the call.