I'm trying to make a class that makes accessing the Adobe Facebook Graph Api easier without all the nitty gritty stuff that goes in. This is how I go about it: I have one FbNewLib.as and a Test.as as well as the main test.fla.
In the FbNewLib.as, I have the init line to initialize the Facebook api.
package
{
import com.facebook.graph.Facebook;
import com.facebook.graph.data.FacebookSession;
import flash.external.ExternalInterface;
public class FbNewLib
{
protected static const app_id:String = "155006914551234" //replace with App Id;
public var scope:String;
public var userId:String;
public var testString:String;
var session:FacebookSession;
public function FbNewLib()
{
trace("FbNewLib initiated.")
}
public function initFacebookLib(scopeString:String):void
{
scope = scopeString;
testString = "testString";
Facebook.init(app_id, onInit);
}
protected function onInit(result:Object, fail:Object):void
{
trace("onInit");
testString = "testStringONINIT";
if (result)
{ //already logged in because of existing session
trace ("On init, already logged in.");
testString = "testStringSUCCESSFUL";
session = Facebook.getSession();
userId = session.uid;
}
else
{
trace("On init, not logged in.");
testString = "testStringUNSUCCESSFUL";
}
}
In my Test.as:
public function Test()
{
trace("connected to test.fla");
init_btn.addEventListener(MouseEvent.CLICK, initFbLib);
}
private function initFbLib(e:MouseEvent):void
{
gotoAndStop(2);
var fb:FbNewLib = new FbNewLib();
fb.initFacebookLib("read_stream");
textBox_txt.text = fb.scope + " " + fb.userId + " " + fb.testString;
}
However, when I test it in my testApp in my testApp in Facebook, the Facebo开发者_StackOverflow社区ok.init() doesn't work. What's in the textbox_txt is just "read_stream, null, testString"
Can anyone help? Is this even possible? Thanks!
this is a way to connect to Facebook using Graph API. Everything is explained in comment. This is actually the connecting to facebook, no posting to walls or anything. That part can be found below this class.
package com.DAL
{
import com.facebook.graph.Facebook;
import flash.events.Event;
import com.facebook.graph.data.FacebookSession;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import com.fbLoginButton;
import com.adobe.serialization.json.JSON;
public class FBConnect extends EventDispatcher
{
/******************************************
* Variables
******************************************/
private var _applicationID:String;
private var _extendedPermissions:Object;
/******************************************
* Constants
******************************************/
public static const CONNECTED:String = "CONNECTED";
/******************************************
* Properties
******************************************/
public function get applicationID():String
{
return _applicationID;
}
/******************************************
* Constructor
******************************************/
public function FBConnect()
{
super();
//Set applicationid
_applicationID = "YOUR_ID";
//Set permissions to ask for
_extendedPermissions = {perms:"read_stream, publish_stream, user_about_me, read_friendlists, user_photos"};
//Initialize facebook
Facebook.init(_applicationID);
}
/******************************************
* Methods
******************************************/
public function login(e:MouseEvent):void
{
Facebook.login(handleLogin, _extendedPermissions);
}
private function handleLogin(response:Object, fail:Object):void
{
dispatchEvent(new Event(CONNECTED));
}
}
}
That should take care of connecting to facebook. If you want to post to walls or anything, you can find a small example below.
/******************************************
* Constructor
******************************************/
public function FBLogic()
{
super();
_connect = new FBConnect();
_connect.addEventListener(FBConnect.CONNECTED, startLoaders);
initLoaders();
}
/******************************************
* Methods
******************************************/
...
public function post(message:String):void
{
var _params:Object = new Object();
_params.access_token = Facebook.getSession().accessToken;
_params.message = message;
Facebook.api("/" + _userID + "/feed", messagePosted, _params, "POST");
}
public function messagePosted(response:Object, fail:Object):void
{
dispatchEvent(new Event(MESSAGEPOSTED));
}
public function login(e:MouseEvent):void
{
var _loginButton:fbLoginButton = e.target as fbLoginButton;
_loginButton.alpha = 0;
_loginButton.visible = false;
_connect.login(e);
}
If this doesn't do the trick you might have forgotten to add some code to your html file. Be sure to add the following code to the head of your html file:
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
And you also need a div called fb-root, declared like this.
<body>
<div id="fb-root"></div>
<div id="flashContent">
</div>
</body>
It's just about you have to set your domain and canvas URL right in your application settings. Otherwise, you won't be able to see any errors when facebook.init() runs and it won't get into that function.
精彩评论