2011年5月1日 星期日

AS3 Class 懶人系列 之 阿Cain的 FaceBook API 2.0 讓你快速上手FB活動(二) -取得使用者資料

使用這登入與認證後,我們就可以開始使用fb的api了。
以下為了方便使用,我依然是把一些常用的功能包裝起來。

比方說,取得使用者資料
fb.getUser();
fb.addEventListener(ResUserEvent.RESULT, onUserRESULT);
fb.addEventListener(ResUserEvent.ERROR,onERROR);
//成功取回資料
private function onUserRESULT(e:ResUserEvent):void 
{
     cont.text += "about: "+e.user.about +"\n";
     cont.text += "bio: "+e.user.bio +"\n";
     cont.text += "birthday: "+e.user.birthday +"\n";
     cont.text += "education: "+ JSON.encode(e.user.education) +"\n";
     cont.text += "email: "+e.user.email +"\n";
     cont.text += "first_name: "+e.user.first_name +"\n";
     cont.text += "gender: "+e.user.gender +"\n";
     cont.text += "hometown: "+e.user.hometown +"\n";
     cont.text += "interested_in: "+e.user.interested_in +"\n";
     cont.text += "last_name: "+e.user.last_name +"\n";
     cont.text += "link: "+e.user.link +"\n";
     cont.text += "locale: "+e.user.locale +"\n";
     cont.text += "location: "+e.user.location +"\n";  
     cont.text += "name: "+e.user.name +"\n";
     cont.text += "political: "+e.user.political +"\n";
     cont.text += "quotes: "+e.user.quotes +"\n";
     cont.text += "relationship_status: "+e.user.relationship_status +"\n";
     cont.text += "religion: "+e.user.religion +"\n";
     cont.text += "significant_other: "+e.user.significant_other +"\n";
     cont.text += "third_party_id: "+e.user.third_party_id +"\n";
     cont.text += "timezone: "+e.user.timezone +"\n";
     cont.text += "uid: "+e.user.uid +"\n";
     cont.text += "updated_time: "+e.user.updated_time+"\n";
     cont.text += "username: "+e.user.username+"\n";
     cont.text += "verified: "+e.user.verified+"\n";
     cont.text += "website: "+e.user.website+"\n";
     cont.text += "work: "+JSON.encode(e.user.work)+"\n";
}
//取回資料失敗
private function onERROR(e:FaceBook_Event):void 
{
     cont.text += e.error_msg + "\n";
     cont.text += e.failJSON + "\n";
}
getUser()這個方法可以回傳目前登入的使用者資料,當然你也可以傳入某個朋友(uid),可以查詢某個特定好友的資料。
如果取回成功,會回傳ResUserEvent.RESULT事件,這個事件裡面會有一個user屬性,相關的資料都會放在裡面(我都整理好了XD)。
如果取回失敗,會回傳ResUserEvent.ERROR事件,錯誤訊息會在error_msg屬性裡,或這你可以直接查看failJSON屬性,它會用JSON結構輸出整個錯誤訊息。

有時候有些欄位會回傳空值,有可能是一開始就沒有填上資料,或者是一開始認證沒有作,比方說你想取得e-mail,如果你登入時沒有作以下認證,也是取不到的。
fb.login(Permissions.ABOUT_ME + "," + Permissions.MAIL);
Permissions.ABOUT_ME 是取得本身相關資料
Permissions.MAIL 是同意取得mail
AS3 Class 懶人系列 之 阿Cain的 FaceBook API 2.0 讓你快速上手FB活動(三) -取得好友列表

13 則留言:

louis 提到...

Like !

erika 提到...

你好,感謝你提供的API,
不知為何,我都讀取不到使用者的uid,
可是可以取得它的email等資訊,
不知道我是否有遺漏什麼程式?

Unknown 提到...

程式貼上來吧...用說的我也只能猜而已,連uid都讀不到的話,真的有點怪,請先確定你的html上面必要的語法都有加,還有你的 app id 跟網址設定都沒有錯誤。另外想取得e-mail務必先認證e-mail的使用者授權,不然你會讀到空值

erika 提到...

程式法於下網址:
https://docs.google.com/document/d/1Yz2kT0TUtZw4LBK3fc3_rMQTlUM_i_fowMJW1JWL7PA/edit?hl=en_US

謝謝你的回答

Unknown 提到...

function onLOGIN(e:FaceBook_Login):void{
trace(e.uid);
}

在以上位置取uid取得到嗎?

erika 提到...

在onLOGIN還是無法取得

Unknown 提到...

先測試一下,在一開始的 onINIT_SUCCESS
是否有進去這個function... 順道把fb.hasLogin印出來看看裡面是什麼。
如果到這裡都有問題,那有可能是html少加了什麼...

我剛才自己測試是沒有問題的
另外你問的headImage
https://graph.facebook.com/你的uid/picture 就可以取得

erika 提到...
網誌管理員已經移除這則留言。
Unknown 提到...

trace("id:"+e.user.uid) 改成 trace("id:"+e.uid)

erika 提到...

會有錯誤訊息,未定義的屬性uid

Unknown 提到...
作者已經移除這則留言。
Unknown 提到...

http://dl.dropbox.com/u/19304009/blog/FBFB.rar APP ID改成你的,自己輸出試一下,這個我用是可以取得uid的

erika 提到...

謝謝你,已經可以取得uid了,
所以uid無法在e:ResUserEvent事件下取得嗎?
另外,假使我一開始登出狀態,
登入後,它無法馬上取得資訊(我用GraphAPI也是如此)。
謝謝你的解答,感激不盡阿!!!