Facebook.init方法没有执行

时间:2012-02-13 22:01:00

标签: php flash actionscript-3

我是使用Facebook Graph API for AS3的总菜鸟。我有一个Flash游戏,想法是将它上传到Facebook,一切似乎都没问题(当进入Facebook应用程序时,swf显示完美,我可以发布Facebook用户ID,用户名和分数到游戏所在的服务器中的数据库。我打算让游戏自动发布给用户提供他/她得分的内容,但经过大量测试后我发现该方法根本没有运行。

以下是尝试将得分发布到用户新闻Feed的.as文件示例:

    package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Stage;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequestMethod;
    import com.facebook.graph.Facebook;
    import com.adobe.serialization.json.JSON;
    import flash.utils.setTimeout;
    import flash.net.navigateToURL;

    public class GameOver extends MovieClip
    {
        private var user_id : String = "";
        private var user_name : String = "";

        // these are the api and secret keys you should be used to
        var api_key:String="MY_API_KEY";



        function GameOver()
        {
            var newscore = Main.scorenum;
            finalScore.text = newscore;
            getFacebookID();
            Facebook.init(api_key);
            var opts:Object = {scope:"publish_stream, email"};
            Facebook.login(onLogin,opts);

        }           

        function onLogin(session:Object, fail:Object):void 
        {
            if(session!=null)
            {
                post();             
            }
            else
            {
                trace("Login Failed");
            }
        }

        public function post():void
        {
            var params:Object = new Object;
            //params.access_token = Facebook.getAuthResponse().accessToken;
            params.message = 'Hello World';
            params.caption = "Hello World";
            params.description = "Hello World";
            params.picture="http://1.bp.blogspot.com/-0Hn3AjTJj6U/TZHe3ragXGI/AAAAAAAAA1M/_SBk3dx61EE/s1600/med_funny-cat.jpg";
            params.name="Hello World";
            params.link="http://1.bp.blogspot.com/-0Hn3AjTJj6U/TZHe3ragXGI/AAAAAAAAA1M/_SBk3dx61EE/s1600/med_funny-cat.jpg";
            var methodInput = 'feed';           
            Facebook.api("/"+user_id+"/feed",submitPostHandler,params,"POST");
        }

        private function submitPostHandler(result:Object, fail:Object):void {
            trace("\n\nRESULT:\n" + JSON.encode(result));
            puntajeFinal.text = "RESULT:" + JSON.encode(result) + user_name;
        }

这是发布.swf的php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<div id="fb-root"></div>
<div id="chimpInvader">
            <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
</div>
<div id="flashContent">
<!--<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="600" height="700">
  <param name="SRC" value="chimpInvader.swf" />
  <embed src="chimpInvader.swf" width="600" height="700" />
</object>-->
</div>
<script>
FB.init({appId  : '336824736350079', status : true, cookie : true, xfbml  : true });
    FB.Canvas.setAutoResize(false);
    FB.Canvas.setSize({ height: 720, width: 600 });
    swfobject.embedSWF("chimpInvader.swf", "chimpInvader", "600", "700", "11.1", null, null, null, null);
</script>

</body>
</html>

我希望你能帮助我,抱歉我糟糕的英语。

1 个答案:

答案 0 :(得分:0)

您正在使用Facebook javascript库通过Facebook验证用户身份。 因此,为了让您的应用程序使用Flash,您需要具有javascript API请求的其他权限才能将故事发布到用户的Feed。

当您请求扩展权限并且用户授予它们时,javascript api将返回访问令牌。这将是一个长串。将此字符串传递给flash,因为在将新闻故事发布到Facebook时,flash需要包含此访问令牌。

当我一起使用facebook和flash时,下面是我喜欢的方式。

<script src="js/swfobject.js"></script>
<script>
        // make it safe to use console.log always
        (function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
        {console.log();return window.console;}catch(err){return window.console={};}})());
        // Set up SWF Address
        var flashvars = {
        };
        var params = {
            menu: "false",
            scale: "noScale",
            allowFullscreen: "true",
            allowScriptAccess: "always",
            bgcolor: "#000000",
            wmode: "transparent" // can cause issues with FP settings & webcam
        };
        var attributes = {
            id:"YourEmbeddedSwf"
        };
        swfobject.embedSWF(
            "YourEmbeddedSwf.swf", 
            "altContent", "520", "724", "10.0.0", 
            "expressInstall.swf", 
            flashvars, params, attributes);
    </script>
</head>

<body>
<div id="fb-root"></div>
<script>
// Load Facebook API
    window.fbAsyncInit = function() {
      FB.init({ appId: 'PLACE YOUR APP ID HERE', 
            channelUrl : '//yoursite.com/channel.html',
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true});
    // Additional initialization code here
  };
  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));

   // Custom Facebook Calls.
   // Call getAccessToken() from flash using External Interface,
    function getAccessToken(){
       FB.login(function(response) {
       if (response.authResponse) {
         var access_token =   FB.getAuthResponse()['accessToken'];
         //Log your access token so you can be sure the javascript is worjking,.
         console.log('Access Token = '+ access_token);
          var swf = document.getElementById("YourEmbeddedSwf");
          // Call a flash function exposed with External Interface.
          swf.setAccessToken(access_token);
         });
       } else {
         console.log('User cancelled login or did not fully authorize.');
       }
     }, {scope: ''});
   }
    // Get Swf 
    function getSwf(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
    }
</script> 
<!-- Embed the swf -->
<div id="altContent">
  <h1>Alternate Content</h1>
  <p><a href="http://www.adobe.com/go/getflashplayer">Get Adobe Flash player</a></p>
</div></body>
</html>