Search code examples
facebookactionscript-3facebook-graph-apifacebook-access-token

as3-fb-api: how is it possible to have an accesstoken BEFORE Facebook.init()


the Facebook.init method of the as3-facebook-api (for flash) allows for an optional accesstoken parameter.

But the whole point of the .init(...) method is to, well, init everything ... accesstokens are a byproduct of the init phase.

So how is it possible to even have an accesstoken in the first place.

(Please do not respond with a rhetorical answer ... I am looking for a app-practical answer ... thanks).


Solution

  • The access token is valid for a specific amount of time, so technically you could reuse it. That, or you could have connected to Facebook using another language and have a token from that (more common than you'd think)

    Edit 24/05/2012

    Ok, After running some tests, and looking at the code in the Facebook AS3, this is what I have:

    • You're right in that you have to call Facebook.init() first, before you do anything. Otherwise, you just get errors everywhere
    • Passing an accessToken won't do anything unless you also pass and options Object with status = false (so your init() call becomes Facebook.init( APP_ID, this._onInit, {status:false}, myAccessToken);)
    • Basically what it does is inits the lib, but doesn't call the getLoginStatus() external event, so you're saving a few calls and your program starts quicker - that's about it as far as I could tell.
    • If you don't care about the lib, and you have an access token, technically you could bypass init() altogether, and just append ?access_token=[myToken] to the end of your urls when you call api() (e.g. Facebook.api( "/me?access_token=" + myAccessToken, this._onInfo );) - or skip the lib entirely, and just make a URLLoader request to the url

    A simple example showing all of this. Create your app as normal, substitute your app id, and connect normally. It'll trace out your uid and access token. Copy the token, launch the app again, and paste the token into the input area. It'll now use this when you init() again.

    package 
    {
        import com.facebook.graph.data.FacebookAuthResponse;
        import com.facebook.graph.Facebook;
        import flash.display.Sprite;
        import flash.events.KeyboardEvent;
        import flash.text.TextField;
        import flash.text.TextFieldType;
        import flash.text.TextFormat;
        import flash.ui.Keyboard;
        import flash.utils.getQualifiedClassName;
    
        /**
         * Test facebook connect, showing the init() call with and without an access token. Make a normal
         * connection, then copy the access token, and reload. paste your token into the input area to use
         * it for the init() call
         * @author Damian Connolly
         */
        public class Main extends Sprite 
        {
            private static const APP_ID:String = "[SET_YOUR_APP_ID]";
    
            private var m_tf:TextFormat     = new TextFormat( "Trebuchet Ms", 9 );
            private var m_input:TextField   = null; // for adding the token
            private var m_log:TextField     = null; // for logging our messages
    
            public function Main():void 
            {
                // create our input
                this.m_input                    = new TextField;
                this.m_input.defaultTextFormat  = this.m_tf;
                this.m_input.border             = true;
                this.m_input.background         = true;
                this.m_input.type               = TextFieldType.INPUT;
                this.m_input.text               = " ";
                this.m_input.width              = 700.0;
                this.m_input.height             = this.m_input.textHeight + 4.0;
                this.m_input.text               = "";
                this.m_input.x = this.m_input.y = 10.0;
                this.addChild( this.m_input );
    
                // log and add our mouse listeners
                this._log( "Press [SPACE] to init facebook connection, [CTRL] to login, [SHIFT] to get data" );
                this._log( "Copy a pre-existing token into the textfield above to use it in the init() call" );
                this.stage.addEventListener( KeyboardEvent.KEY_DOWN, this._onKeyDown );
            }
    
            // keyboard listener
            private function _onKeyDown( e:KeyboardEvent ):void
            {
                if ( e.keyCode == Keyboard.SPACE )
                    this._init();
                else if ( e.keyCode == Keyboard.CONTROL )
                    this._login();
                else if ( e.keyCode == Keyboard.SHIFT )
                    this._getData();
            }
    
            // init our facebook api
            private function _init():void
            {
                // use our token if we have one
                var token:String = ( this.m_input.text != "" ) ? this.m_input.text : null;
    
                this._log( "---------- Initing facebook with token '" + token + "'" );
                if ( token == null )
                    Facebook.init( Main.APP_ID, this._onInit );
                else
                {
                    var options:* = { status:false };
                    Facebook.init( Main.APP_ID, this._onInit, options, token );
                }
            }
    
            // callback for init
            private function _onInit( success:*, fail:* ):void
            {
                this._log( "Callback for init 2! success: " + success + ", fail: " + fail );
                this._log( "Success: " + getQualifiedClassName( success ) );
                this._log( "Fail: " + getQualifiedClassName( fail ) );
    
                var response:FacebookAuthResponse = success as FacebookAuthResponse;
                if ( response != null )
                    this._log( "UID: " + response.uid + ", access token: " + response.accessToken );
    
                this._log( ( success != null ) ? "Logged in" : "Not logged in" );
            }
    
            // logs into the app if we need to
            private function _login():void
            {
                this._log( "---------- Logging in" );
                Facebook.login( this._onLogin );
            }
    
            // callback for the login
            private function _onLogin( success:*, fail:* ):void
            {
                this._log( "Callback for _onLogin! success: " + success + ", fail: " + fail );
                this._log( "Success: " + getQualifiedClassName( success ) );
                this._log( "Fail: " + getQualifiedClassName( fail ) );
    
                var response:FacebookAuthResponse = success as FacebookAuthResponse;
                if ( response != null )
                    this._log( "UID: " + response.uid + ", access token: " + response.accessToken );
            }
    
            // gets our data
            private function _getData():void
            {
                this._log( "---------- getting data with url '/me'" );
                Facebook.api( "/me", this._onGetData );
            }
    
            // callback for our data
            private function _onGetData( success:*, fail:* ):void
            {
                this._log( "Callback for _onLogin! success: " + success + ", fail: " + fail );
                this._log( "Success: " + getQualifiedClassName( success ) );
                for ( var key:* in success )
                    this._log( "  key '" + key + "' = " + success[key] );
    
                this._log( "Fail: " + getQualifiedClassName( fail ) );
            }
    
            // logs a message to the console and our textfield
            private function _log( msg:String ):void
            {
                // create our log if needed
                if ( this.m_log == null )
                {
                    this.m_log                      = new TextField;
                    this.m_log.defaultTextFormat    = this.m_tf;
                    this.m_log.border               = true;
                    this.m_log.background           = true;
                    this.m_log.width                = 700.0;
                    this.m_log.height               = 200.0;
                    this.m_log.x                    = this.m_input.x;
                    this.m_log.y                    = this.m_input.y + this.m_input.height + 5.0;
                    this.m_log.wordWrap             = true;
                    this.addChild( this.m_log );
                }
                trace( msg );
                this.m_log.appendText( msg + "\n" );
            }
    
        }
    
    }