使用facebook4j获取有关页面的信息

时间:2014-05-26 23:41:00

标签: java facebook facebook-graph-api facebook-page facebook-graph-api-v2.0

是否有可能在about部分获取页面信息的String? 示例:https://www.facebook.com/FacebookDevelopers 以下是信息:“使用Facebook构建,发展和利用您的应用获利。https://developers.facebook.com/

我发现Facebook图表api通过Page上的Field about支持此功能。

提前感谢您的帮助!

祝你好运, 星

1 个答案:

答案 0 :(得分:0)

您可以在以下代码段中获取java中的Facebook页面信息:

private static final String FacebookURL_PAGES = "me/accounts?fields=access_token,category,id,perms,picture{url},can_post,is_published,cover,fan_count,is_verified,can_checkin,global_brand_page_name,link,country_page_likes,is_always_open,is_community_page,new_like_count,overall_star_rating,name";   

public List<FacebookPageModel> getPages(String accessToken) throws FacebookException, JSONException {

    JSONObject posts = getBatch(accessToken,FacebookURL_PAGES);
    Gson g =new Gson();
    System.out.println(posts);
    JSONArray postsData =  posts.getJSONArray("data");
    System.out.println(g.toJson(postsData));

    return getPageList(postsData);
}

private JSONObject getBatch(String accessToken, String url) throws FacebookException{
    Gson g = new Gson();
    Facebook facebook = getFacebook(accessToken);
    BatchRequests<BatchRequest> batch = new BatchRequests<BatchRequest>();
    batch.add(new BatchRequest(RequestMethod.GET, url));
    List<BatchResponse> results = facebook.executeBatch(batch);
    BatchResponse result2 = results.get(0);

    System.out.println(g.toJson(result2.asJSONObject()));

    return result2.asJSONObject();
}

private Facebook getFacebook(String accessToken){
    if(accessToken == null){
        System.out.println("Access Token null while request for Facebook Instance !");
        return null;
    }

    Facebook facebook = new FacebookFactory().getInstance();
    facebook.setOAuthAppId(appId, appSecret);
    facebook.setOAuthPermissions("email,publish_stream, publish_actions");
    facebook.setOAuthAccessToken(new AccessToken(accessToken, null));

    return facebook;
}

private List<FacebookPageModel> getPageList(JSONArray pagesData) throws JSONException{
    Gson g = new Gson();
    List<FacebookPageModel> pages = new ArrayList<FacebookPageModel>();
    SimpleDateFormat desiredFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = null;
    for (int i = 0; i < pagesData.length(); i++) {
        FacebookPageModel obj = new FacebookPageModel();
        JSONObject page = pagesData.getJSONObject(i);
        System.out.println(g.toJson(page));
        try{
            obj.setAccessToken(page.getString("access_token"));
        }catch(Exception ee){
            obj.setAccessToken(null);
        }
        try{
            obj.setCategory(page.getString("category"));
        }catch(Exception ee){
            obj.setCategory(null);
        }
        try{
            obj.setId(page.getString("id"));
        }catch(Exception ee){
            obj.setId(null);
        }
        try{
            obj.setName(page.getString("name"));
        }catch(Exception ee){
            obj.setName(null);
        }
        try{
            obj.setCanPost(page.getBoolean("can_post"));
        }catch(Exception ee){
            obj.setCanPost(false);
        }
        try{
            JSONObject picture = page.getJSONObject("picture");
            JSONObject pictureData = picture.getJSONObject("data");
            obj.setPageProfilePic(pictureData.getString("url"));
        }catch(Exception ee){
            obj.setCanPost(false);
        }
        try{
            obj.setPublished(page.getBoolean("is_published"));
        }catch(Exception ee){
            obj.setPublished(false);
        }
        try{
            obj.setFanCount(page.getLong("fan_count"));
        }catch(Exception ee){
            obj.setFanCount(0L);
        }
        try{
            obj.setVerified(page.getBoolean("is_verified"));
        }catch(Exception ee){
            obj.setVerified(false);
        }
        try{
            obj.setCanCheckin(page.getBoolean("can_checkin"));
        }catch(Exception ee){
            obj.setCanCheckin(false);
        }
        try{
            obj.setGlobalBranPageName(page.getString("global_brand_page_name"));
        }catch(Exception ee){
            obj.setGlobalBranPageName(null);
        }
        try{
            obj.setPageLink(page.getString("link"));
        }catch(Exception ee){
            obj.setPageLink(null);
        }
        try{
            obj.setNewLikeCount(page.getLong("new_like_count"));
        }catch(Exception ee){
            obj.setNewLikeCount(0L);
        }
        try{
            obj.setOverallStarRating(page.getLong("overall_star_rating"));
        }catch(Exception ee){
            obj.setOverallStarRating(0L);
        }
        try{
            obj.setOverallStarRating(page.getLong("overall_star_rating"));
        }catch(Exception ee){
            obj.setOverallStarRating(0L);
        }

        pages.add(obj);
    }
    System.out.println(g.toJson(pages));
    return pages;
}

public class FacebookPageModel {

//access_token
private String accessToken;
//name
private String name;
//id
private String id;
//category
private String category;
// can_post
private boolean canPost;

private String pageProfilePic;
// is_published
private boolean isPublished;
// fan_count
private long fanCount;
// is_verified
private boolean isVerified;
// can_checkin
private boolean canCheckin;
// global_brand_page_name
private String globalBranPageName;
// link
private String pageLink;
// new_like_count
private long newLikeCount;
// overall_star_rating
private long overallStarRating;

public boolean isCanPost() {
    return canPost;
}
public void setCanPost(boolean canPost) {
    this.canPost = canPost;
}
public String getPageProfilePic() {
    return pageProfilePic;
}
public void setPageProfilePic(String pageProfilePic) {
    this.pageProfilePic = pageProfilePic;
}
public String getAccessToken() {
    return accessToken;
}
public void setAccessToken(String accessToken) {
    this.accessToken = accessToken;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getCategory() {
    return category;
}
public void setCategory(String category) {
    this.category = category;
}
public boolean isPublished() {
    return isPublished;
}
public void setPublished(boolean isPublished) {
    this.isPublished = isPublished;
}
public long getFanCount() {
    return fanCount;
}
public void setFanCount(long fanCount) {
    this.fanCount = fanCount;
}
public boolean isVerified() {
    return isVerified;
}
public void setVerified(boolean isVerified) {
    this.isVerified = isVerified;
}
public boolean isCanCheckin() {
    return canCheckin;
}
public void setCanCheckin(boolean canCheckin) {
    this.canCheckin = canCheckin;
}
public String getGlobalBranPageName() {
    return globalBranPageName;
}
public void setGlobalBranPageName(String globalBranPageName) {
    this.globalBranPageName = globalBranPageName;
}
public String getPageLink() {
    return pageLink;
}
public void setPageLink(String pageLink) {
    this.pageLink = pageLink;
}
public long getNewLikeCount() {
    return newLikeCount;
}
public void setNewLikeCount(long newLikeCount) {
    this.newLikeCount = newLikeCount;
}
public long getOverallStarRating() {
    return overallStarRating;
}
public void setOverallStarRating(long overallStarRating) {
    this.overallStarRating = overallStarRating;
}
}
相关问题