简体   繁体   English

许可facebook sdk Android

[英]Permission facebook sdk Android

Working on Facebook . Facebook工作。 Unable to get publish_actions permission. 无法获得publish_actions权限。 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 onActivityResult执行此操作

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. Facebook sdk仅返回读取权限,但不返回写入权限。 You can user the "/me" endpoint to get all the permissions. 您可以使用“ / me”端点获取所有权限。

    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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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