as3-fb-api:如何在Facebook.init()之前进行一次访问

时间:2012-05-20 10:09:47

标签: facebook actionscript-3 facebook-graph-api facebook-access-token

as3-facebook-api(用于flash)的Facebook.init方法允许使用可选的accessstoken参数。

但.init(...)方法的重点是,初始化一切...... accessstokens是init阶段的副产品。

那么如何才能在第一时间进行访问。

(请不要回答一个修辞的答案......我正在寻找一个应用实用的答案......谢谢)。

1 个答案:

答案 0 :(得分:2)

访问令牌在特定时间内有效,因此从技术上讲,您可以重复使用它。那,或者你可以使用另一种语言连接到Facebook,并从中获得一个令牌(比你想象的更常见)

编辑24/05/2012

好的,在运行一些测试并查看Facebook AS3中的代码之后,这就是我所拥有的:

  • 你是对的,你必须在做任何事之前先调用Facebook.init()。否则,你到处都会遇到错误
  • 传递accessToken将不会执行任何操作,除非您也传递Object status = false init(),因此Facebook.init( APP_ID, this._onInit, {status:false}, myAccessToken);来电变为getLoginStatus()
  • 基本上它的功能是在lib中,但不会调用init()外部事件,所以你节省了几个电话,你的程序启动得更快 - 就我所知,这就是它。
  • 如果您不关心lib,并且您有访问令牌,从技术上讲,您可以完全绕过?access_token=[myToken],只需在调用{{{{}}时将api()附加到您的网址末尾1}}(例如Facebook.api( "/me?access_token=" + myAccessToken, this._onInfo );) - 或完全跳过lib,只需向网址发出URLLoader个请求

显示所有这些的简单示例。正常创建您的应用,替换您的应用ID,并正常连接。它会找出你的uid和访问令牌。复制令牌,再次启动应用程序,然后将令牌粘贴到输入区域。现在,当您再次init()时,它会使用此功能。

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" );
        }

    }

}