Facebook登录与LoginManager loginWithPublishPermissions无法正常工作

时间:2015-12-15 17:45:28

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

我正在尝试在我的Android应用中集成Facebook Android SDK。我使用此块来允许用户登录。

    FacebookSdk.sdkInitialize(this.getApplicationContext());
    //some more code
    callbackManager = CallbackManager.Factory.create();
    pendingAction = PendingAction.POST_UPDATE;
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.e(TAG, "OnSuccess for RegisterCallback");

                    if (AccessToken.getCurrentAccessToken().getPermissions().contains(PERMISSION)) {
                        Log.e(TAG, "I want to  Post on Facebook");
                        postStatusUpdate("Hello World Post!");
                        updateUI();

                    } else {
                        Log.e(TAG, "I want more permissions");
                        requestPublishPermissions();
                    }
                }


                @Override
                public void onCancel() {
                    Log.e(TAG, "OnCancel for RegisterCallback");
                    if (pendingAction != PendingAction.NONE) {
                        showAlert();
                        pendingAction = PendingAction.NONE;
                    }
                    updateUI();
                }

                @Override
                public void onError(FacebookException exception) {
                    Log.e(TAG, "OnError for RegisterCallback");
                    if (pendingAction != PendingAction.NONE
                            && exception instanceof FacebookAuthorizationException) {
                        showAlert();
                        pendingAction = PendingAction.NONE;
                    }
                    updateUI();
                }

                private void showAlert() {
                    new AlertDialog.Builder(ShareFacebookActivity.this)
                            .setTitle(R.string.cancelled)
                            .setMessage(R.string.permission_not_granted)
                            .setPositiveButton(R.string.ok, null)
                            .show();
                }
            });
        requestPublishPermissions();

` 正如您所看到的,大多数代码都是从Facebook Sample

复制而来的

我尝试使用发布权限登录,但是当用户输入用户名/密码时,它会显示屏幕,好像我没有添加发布权限。它显示消息“这不允许应用程序在Facebook上发布。”

当我尝试使用此代码添加发布权限时

LoginManager.getInstance()
                .setDefaultAudience(DefaultAudience.FRIENDS)
                .logInWithPublishPermissions(this, Arrays.asList(PERMISSION));

它表明您已经授权此应用。 我该怎么办?请在投票前发表评论。

1 个答案:

答案 0 :(得分:0)

您需要先检查您是否已获得代表用户发布的权限..这样的事情......

run()

如果您需要获得许可,请使用以下代码段

    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());
                        Toast.makeText(YourActivity.this, "Error occurred while connecting", Toast.LENGTH_SHORT).show();
                    }

                }
            }
    );
    request.executeAsync();