AS3:Facebook身份验证示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3:Facebook身份验证示例相关的知识,希望对你有一定的参考价值。

I wanted an easy way to authenticate a user before making calls to Facebook. It's not perfect but it certainly makes things easier. Note: This is not a class!
  1. import com.facebook.commands.friends.GetFriends;
  2. import com.facebook.data.users.FacebookUser;
  3. import com.facebook.data.friends.GetFriendsData;
  4. import com.facebook.events.FacebookEvent;
  5. import com.facebook.Facebook;
  6. import com.facebook.net.FacebookCall;
  7. import com.facebook.utils.FacebookSessionUtil;
  8. import flash.display.Stage;
  9. import flash.display.LoaderInfo;
  10. import mx.controls.Alert;
  11.  
  12. private var API_KEY:String = "API_KEY";
  13. private var SECRET_KEY:String = "SECRET_KEY";
  14.  
  15. private var fbook:Facebook;
  16. private var fbLoaderInfo:LoaderInfo;
  17. private var fbSession:FacebookSessionUtil;
  18. private var currentMethod:Function;
  19.  
  20. private function initApp():void
  21. {
  22. fbLoaderInfo = stage.loaderInfo;
  23.  
  24. getFriends();
  25. }
  26.  
  27. /**************************************
  28.  * Actions
  29. **************************************/
  30. private function getFriends():void
  31. {
  32. //If the user is not logged in, force one
  33. if( !isAuthenticated() ){ fbLogin( arguments.callee ); return }
  34.  
  35. var call:FacebookCall = fbook.post( new GetFriends( ) );
  36. call.addEventListener( FacebookEvent.COMPLETE, getFriendsCompleteHandler, false, 0, true );
  37. }
  38.  
  39. private function getFriendsCompleteHandler( e:FacebookEvent ):void
  40. {
  41. if( e.error ) trace( "getFriendsCompleteHandler: " + e.error);
  42.  
  43. var friendsData:GetFriendsData = e.data as GetFriendsData;
  44.  
  45. //Iterate through the albumsResponseData.albumCollection to get the titles
  46. for( var i:uint = 0; i < friendsData.friends.length; i++ ){
  47. var friendID:Number = friendsData.friends.getItemAt(i).uid;
  48. var facebookUser:FacebookUser = friendsData.friends.getUserById( friendID );
  49. trace( "Facebook User Name: " + facebookUser.name );
  50. }
  51.  
  52. }
  53.  
  54.  
  55. /**************************************
  56.  * Facebook Authentication
  57. **************************************/
  58. private function fbLogin( method:Function ):void
  59. {
  60. currentMethod = method;
  61.  
  62. fbSession = new FacebookSessionUtil( API_KEY, SECRET_KEY, loaderInfo );
  63. fbSession.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true );
  64. fbSession.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true );
  65. fbook = fbSession.facebook;
  66. fbSession.login();
  67. }
  68.  
  69. private function isAuthenticated():Boolean
  70. {
  71. if( fbSession == null || !fbook.is_connected ) return false;
  72.  
  73. return true;
  74. }
  75.  
  76. /**************************************
  77.  * Facebook Event Handlers
  78. **************************************/
  79. private function fbOnConnectHandler( e:FacebookEvent ):void
  80. {
  81. trace( "You are logged into Facebook" );
  82. //Continue where the user last left off before requiring Authentication
  83. currentMethod();
  84. }
  85.  
  86. private function fbWaitingForLoginHandler( e:FacebookEvent ):void
  87. {
  88. //The user has returned from the log-in page and are now clicking "OK"
  89. var alert:Alert = Alert.show( "Click 'OK' after you've logged in", "Logging In" );
  90. alert.addEventListener( Event.CLOSE, alertCloseHandler, false, 0, true );
  91. alert.addEventListener( Event.CLOSE, fbValidateLogin, false, 0, true );
  92. }
  93.  
  94. private function fbValidateLogin( e:Event ):void
  95. {
  96. fbSession.validateLogin();
  97. }
  98.  
  99. private function alertCloseHandler( e:Event ):void
  100. {
  101. }

以上是关于AS3:Facebook身份验证示例的主要内容,如果未能解决你的问题,请参考以下文章

AS3:Facebook身份验证类

ActionScript 3 AS3:Facebook身份验证类

没有登录按钮的 Facebook 身份验证

超出未经身份验证的使用的每日限制。继续使用需要注册错误

教程4 - 验证和权限

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段