2

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

但是 .init(...) 方法的重点是,嗯,初始化一切......访问令牌是初始化阶段的副产品。

那么怎么可能一开始就拥有一个访问令牌。

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

4

1 回答 1

2

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

编辑 24/05/2012

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

  • 你是对的,你必须先调用 Facebook.init(),然后再做任何事情。否则,你只会到处出错
  • 传递 accessToken 不会做任何事情,除非你也传递和选项Objectstatus = false所以你的init()电话变成Facebook.init( APP_ID, this._onInit, {status:false}, myAccessToken);
  • 基本上它所做的是初始化库,但不调用getLoginStatus()外部事件,所以你节省了一些调用,你的程序启动得更快——据我所知,这就是它。
  • 如果您不关心库,并且您有访问令牌,从技术上讲,您可以完全绕过,并在您调用(例如)时init()附加?access_token=[myToken]到您的网址末尾- 或者完全跳过库,然后发出请求到网址api()Facebook.api( "/me?access_token=" + myAccessToken, this._onInfo );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" );
        }

    }

}
于 2012-05-20T17:56:50.843 回答