as3-facebook-api(针对 flash)的 Facebook.init 方法允许使用可选的 accesstoken 参数。
但是 .init(...) 方法的重点是初始化所有内容...访问 token 是初始化阶段的副产品。
那么,如何才能拥有访问 token 呢?
(请不要用修辞性的答案来回应......我正在寻找一个应用程序实用的答案......谢谢)。
最佳答案
访问 token 在特定时间内有效,因此从技术上讲您可以重复使用它。或者您可以使用另一种语言连接到 Facebook 并从中获得 token (比您想象的更常见)
编辑2012年5月24日
好的,在运行一些测试并查看 Facebook AS3 中的代码后,这就是我所拥有的:
- 你是对的,在做任何事情之前你必须先调用 Facebook.init() 。否则,到处都会出现错误
- 传递 accessToken 不会执行任何操作,除非您还传递和选项
Object
与status = false
(因此您的init()
调用将变为Facebook.init( APP_ID, this._onInit, {status:false}, myAccessToken);
) - 基本上它所做的是初始化库,但不调用
getLoginStatus()
外部事件,因此您可以节省一些调用,并且您的程序启动得更快 - 据我所知,仅此而已。 - 如果您不关心该库,并且您有访问 token ,从技术上讲您可以绕过
init()
总共,只需附加?access_token=[myToken]
当您调用api()
时到您的网址末尾(例如Facebook.api( "/me?access_token=" + myAccessToken, this._onInfo );
) - 或完全跳过库,只需创建URLLoader
请求网址
一个简单的例子展示了这一切。正常创建您的应用程序,替换您的应用程序 ID,然后正常连接。它将追踪您的 uid 和访问 token 。复制 token ,再次启动应用程序,然后将 token 粘贴到输入区域。现在,当您 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" );
}
}
}
关于facebook - as3-fb-api : how is it possible to have an accesstoken BEFORE Facebook. init(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672527/