ActionScript 3 简单的Facebook使用GraphAPI_Web.Swc在As3中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 简单的Facebook使用GraphAPI_Web.Swc在As3中相关的知识,希望对你有一定的参考价值。
package
{
import com.adobe.images.JPGEncoder;
import com.facebook.graph.Facebook;
import com.facebook.graph.data.FacebookAuthResponse;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.Security;
import flash.utils.ByteArray;
public class FacebookGraphAPI extends EventDispatcher
{
private var _permissions : Array;
private var _appID : String ;
private var _appSecret : String ;
private var _userAuthResponse : FacebookAuthResponse = null;
private var _apiSecuredPath:String = "https://graph.facebook.com";
private var _apiUnsecuredPath:String = "http://graph.facebook.com";
private var _userAuth : Boolean = false;
public function FacebookGraphAPI (appID : String, appSecret : String, permissions : Array)
{
_appID = appID;
_appSecret = appSecret;
_permissions = permissions;
Security.loadPolicyFile(_apiSecuredPath + "/crossdomain.xml");
Security.loadPolicyFile(_apiUnsecuredPath + "/crossdomain.xml");
Facebook.init(_appID,handleIinit)
}
private function handleIinit(response:Object, fail:Object):void
{
SimpleFacebook.log.text ="iniciando";
if (response != null)
{
Facebook.api("me/permissions",OnCompleteRequestPermissions);
}
else
{
dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.FINISHED_INIT));
}
}
// check if the user has granted all the nedded permissions
private function OnCompleteRequestPermissions(response : Object, fail : Object):void
{
var askForAllPermissions :Boolean = false
for each (var perm : String in _permissions)
{
if (response[0][perm] == "undefined" || response[0][perm] == null)
{
askForAllPermissions = true;
break;
}
}
if (askForAllPermissions)
{
dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.FINISHED_INIT));
}
else
{
dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.FINISHED_INIT));
AuthorizedUser(Facebook.getAuthResponse() as FacebookAuthResponse);
}
}
private function AuthorizedUser (response : FacebookAuthResponse) : void
{
_userAuthResponse = response;
}
public function Login(e : Event = null) : void
{
if (_userAuthResponse == null)
{
Facebook.login(handleLogin, {scope: _permissions.toString()});
}
else
{
dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.LOGGED_IN));
}
}
private function handleLogin(response:Object, fail:Object = null):void
{
if (Facebook.getAuthResponse().uid != null)
{
dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.LOGGED_IN));
AuthorizedUser(Facebook.getAuthResponse() as FacebookAuthResponse);
}
}
public function PostScore(score : Number, callback : Function) : void
{
var data:Object = new Object();
data.score = score;
data.client_id = _appID;
data.client_secret =_appSecret;
Facebook.api(_userAuthResponse.uid+"/scores/",callback,data,"POST");
}
public function FetchScores(callback : Function) : void
{
Facebook.api(_appID+"/scores/",callback);
/*
function callback(response : Object)
for each(var object : Object in response)
{
object.score;
object.user.name;
object.user.id;
}
*/
}
public function FetchUserImage(userID : String) : URLLoader
{
var pictureUrl: String = Facebook.getImageUrl(userID);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
var request:URLRequest = new URLRequest(pictureUrl);
loader.load(request);
return loader;
/*
FetchUserImage(userID).addEventListener(Event.COMPLETE,EventListener);
function EventListener (e:Event)
{
var loader:Loader = new Loader();
loader.loadBytes(e.currentTarget.data);
addChild(loader);
}
*/
}
public function PostImageToAlbum(message : String,image : DisplayObject,clipRect : Rectangle ) : void
{
var bd:BitmapData = new BitmapData(clipRect.width,clipRect.height,true,00000000);
var AuxBData : BitmapData = new BitmapData(image.width,image.height,true,0);
AuxBData.draw(image,null,null,null,clipRect);
bd.copyPixels(AuxBData,clipRect,new Point(0,0));
var result:Bitmap = new Bitmap(bd);
var params: Object = new Object;
var encoder:JPGEncoder = new JPGEncoder(75);
var bytes:ByteArray = encoder.encode(result.bitmapData);
params.message = message;
params.image = bytes;
params.fileName = "image.jpg";
Facebook.api("/me/photos",null,params,"POST");
}
public function PostToUserWall(userId : String,message : String, name :String,caption : String, description : String, link : String) : void
{
var params: Object = new Object;
params.message = message;
params.name = name;
params.caption = caption;
params.description = description;
params.link = link;
Facebook.api("/"+userId+"/feed",null,params,"POST");
}
}
}
//////////////////////////////////EVENTS CLASS/////////////////////////////////
package
{
import flash.events.Event;
public class FacebookGraphAPIEvent extends Event
{
public static const FINISHED_INIT:String =
"FacebookGraphAPIEventFINISH_INIT";
public static const LOGGED_IN:String =
"FacebookGraphAPIEventLOGGED_IN";
public var error : String = null
public var data : Object = null;
public function FacebookGraphAPIEvent(type : String,data : Object = null, error : String = null)
{
super(type);
this.error = error;
this.data = data
}
}
}
以上是关于ActionScript 3 简单的Facebook使用GraphAPI_Web.Swc在As3中的主要内容,如果未能解决你的问题,请参考以下文章