圖片來源:http://www.roars.in/facebook-application-developer-india.html
請先下載 facebook-java-api
需要的jar有三個
facebook-java-api-3.0.2.jar
facebook-java-api-schema-3.0.2.jar
runtime-0.4.1.5.jar
appID及appSecret接由開發人員應用程式取得 authToken為當驗證FB登入成功時,根據Canvas URL所設定的路徑,導回我們的系統,會自動帶參數auth_token回來。若尚未登入,則跳到fb的登入頁面。
String appID = "XXXXX";
String appSecret = "XXXXX";
String authToken= hsrHttpServletRequest.getParameter("auth_token");
FacebookJaxbRestClient facebookClient = new FacebookJaxbRestClient(appID, appSecret);
if(authToken!=null){
try {
facebookClient.auth_getSession(authToken);
FriendsGetResponse friendsResponse = facebookClient.friends_get();
//取得朋友名單
List friends = friendsResponse.getUid();
LOG.debug("friends = "+friends.size());
//設定要取得的欄位
Set fields = new HashSet();
fields.add(ProfileField.SEX.fieldName()); //性別
fields.add(ProfileField.NAME.fieldName()); //姓名
List users = facebookClient.users_getInfo(friends, fields).getUser();
for(User user:users) {
LOG.debug(user.getSex());
LOG.debug(user.getName());
}
} catch (FacebookException e) {
LOG.debug("取資料發生錯誤");
}
}
