简体   繁体   中英

Permission facebook sdk Android

Working on Facebook . Unable to get publish_actions permission. Using this code:

session.requestNewPublishPermissions(new
Session.NewPermissionsRequest(this, PERMISSION));

any solution...

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private boolean pendingPublishReauthorization = false;

in onActivityResult do this

Session session = Session.getActiveSession();

    if (session != null) {

        // Check for publish permissions
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermsnRequest = new Session.NewPermissionsRequest(
                    this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermsnRequest );
            return;
        }
    }

Also implement this in your class.

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

By doing this you will be able to get all the public permissions. you have to open the session after getting read permissions and then only request for publish permissions.

Facebook sdk only returns read-permission, but not the write permissions. You can user the "/me" endpoint to get all the permissions.

    final Bundle permBundle = new Bundle();
    permBundle.putCharSequence("permission", "publish_actions");
    GraphRequest request = new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/me/permissions", permBundle, HttpMethod.GET,
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse graphResponse) {
                    Log.d(TAG, "response2: " + graphResponse.getJSONObject());
                    try {
                        JSONArray permList = (JSONArray) graphResponse.getJSONObject().get("data");
                        if(permList.length() == 0){
                            // no data for perms, hence asking permission
                            askForFBPublishPerm();
                        }else{
                            JSONObject permData = (JSONObject) permList.get(0);
                            String permVal = (String) permData.get("status");
                            if(permVal.equals("granted")){
                                postToFB();
                            }else{
                                askForFBPublishPerm();
                            }
                        }
                    } catch (JSONException e) {
                        Log.d(TAG, "exception while parsing fb check perm data" + e.toString());
                    }

                }
            }
    );
    request.executeAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM