如何使用Facebook的Android SDK请求发布权限?

时间:2016-06-21 19:16:33

标签: android facebook facebook-graph-api facebook-sdk-4.0

如果不使用任何UI /按钮,如何使用Facebook的Android SDK请求Facebook应用程序的发布权限?我目前正在使用此代码,但它会两次注册回调,但会给我重复的结果。要求发布权限的正确方法是什么?

    List<String> basicPermissions = Arrays.asList("user_friends", "user_games_activity");
    List<String> publishPermissions = Arrays.asList("publish_actions");

    mCallbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().logInWithReadPermissions(this, basicPermissions);
    LoginManager.getInstance().logInWithPublishPermissions(this, publishPermissions);

    LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResults) {

                    new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "/{my-app-id}/scores",
                            null,
                            HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    // my code
                                }
                            }
                    ).executeAsync();

                }

                @Override
                public void onCancel() {

                    Log.e("dd","facebook login canceled");

                }

                @Override
                public void onError(FacebookException e) {



                    Log.e("dd", "facebook login failed error");

                }
            });

1 个答案:

答案 0 :(得分:0)

根据FB,在用户从读取权限请求成功返回之前,您不应该请求发布权限。所以你必须改变它:

private boolean publishPermissionsRequested = false;
private LoginResult readResultSuccess;

private void loginWithFacebook(){
    LoginManager.getInstance().registerCallback(mCallbackManager,
        new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResults) {
                if(!publishPermissionsRequested){
                    publishPermissionsRequested = true;
                    readResultSuccess = loginResults;

                    LoginManager.getInstance()
                            .logInWithPublishPermissions(this, publishPermissions);

                    return;
               }

               onFacebookLoginSuccessful(loginResults);
            }

            @Override
            public void onCancel() { 
                if(publishPermissionsRequested && readResultSuccess != null)
                    onFacebookLoginSuccessful(readResultSuccess);
            }

            @Override
            public void onError(FacebookException e) { { 
                if(publishPermissionsRequested && readResultSuccess != null)
                    onFacebookLoginSuccessful(readResultSuccess);
            }
        });

    LoginManager.getInstance()
            .logInWithReadPermissions(this, basicPermissions);
}

private void onFacebookLoginSuccessful(LoginResult loginResults){
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/{my-app-id}/scores",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                 public void onCompleted(GraphResponse response) {
                      // Your code
                 }
            }
    ).executeAsync();
}

这样,如果他们接受读取权限但拒绝(或失败)发布权限请求,它仍将继续成功。

希望这会指出你正确的方向。