如何从Android应用程序喜欢Facebook帖子

时间:2014-03-07 12:35:40

标签: android facebook permissions facebook-like publish

我已经在我的应用程序中实现了登录身份验证,我知道如何分享,但我对此感到疯狂,因为我永远不会得到积极的回应。那么......我如何通过请求发布权限来获取我的Android应用程序与facebook sdk的帖子?

1 个答案:

答案 0 :(得分:1)

它为我工作。

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "publish_stream");

public void likeThis() {
Session session = Session.getActiveSession();       

if (session != null && session.isOpened()) {
    List<String> permissions = session.getPermissions();
    if (!isSubsetOf(PERMISSIONS, permissions)) {
        pendingPublishReauthorization = true;
        Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);

        if (setPermissions(session, newPermissionsRequest))
            return;
    }

    Request.Callback callback = new Request.Callback() {
        public void onCompleted(Response response) {

            if (response != null) {
                FacebookRequestError error = response.getError();

                if (error != null) {
                    //                      error
                } else {
                    //                      success
                }
            } else {
                MessageToast toast =  MessageToast.getInstance(mContext);
                toast.show("Tente novamente mais tarde", MessageToast.ALERT);
            }
        }
    };

    Request request = new Request(session, "1442581505974136/likes", null, HttpMethod.POST, callback);

    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();
}else{
//      not logged
}

}

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

private boolean setPermissions(final Session session, final NewPermissionsRequest newPermissionsRequest) {

    try {
        Log.i(TAG, "Getting a new permission...");
        session.requestNewPublishPermissions(newPermissionsRequest);

        return true;
    } catch (IllegalStateException e) {
        Log.e("setPermissions","[IllegalStateException] \n" + e.getMessage());


        pendingPublishReauthorization = false;

        return false;
    } catch (UnsupportedOperationException e) {
        Log.e("setPermissions","[UnsupportedOperationException] \n" + e.getMessage());


        pendingPublishReauthorization = false;

        return false;
    }
}

但是有时候,我不喜欢一些fb_id。我不知道为什么,但是..