Facebook分享Android应用程序的权限

时间:2016-01-08 06:07:51

标签: java android facebook share

我需要在我的Android应用中分享facebook上的内容。我可以登录并检索使用个人资料信息,但我无法分享帖子。 shareDialog.registerCallback我的案子没有执行。 对于我的应用中的Facebook分享,我是否需要提供写入权限?如果是,何时给?

我的登录和共享代码,

private CallbackManager callbackManager;

    ShareDialog shareDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.activity_main);

        LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email"));
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {


                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        callbackManager = CallbackManager.Factory.create();
                        shareDialog = new ShareDialog(MainActivity.this);
                        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
                            @Override
                            public void onSuccess(Sharer.Result result) {

                                if (ShareDialog.canShow(ShareLinkContent.class)) {
                                    ShareLinkContent linkContent = new   ShareLinkContent.Builder()
                                            .setContentTitle("Share on fb")

                                            .setContentUrl(Uri.parse("https://www.google.com"))
                                            .build();

                                    shareDialog.show(linkContent);
                                }
                            }

                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onError(FacebookException e) {

                            }
                        });
                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });

               //     LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent   data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

我想要的方式就像点击分享按钮(图片下方)它应该登录并在Facebook上分享,我想我应该使用我自己的自定义界面。

enter image description here

1 个答案:

答案 0 :(得分:0)

是的,你需要publish_actions权限。试试这段代码。

ShareDialog shareDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

setContentView(R.layout.activity_main);

LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email"));
LoginManager.getInstance().registerCallback(callbackManager,
        new FacebookCallback<LoginResult>() {


            @Override
            public void onSuccess(LoginResult loginResult) {
                callbackManager = CallbackManager.Factory.create();
                shareDialog = new ShareDialog(MainActivity.this);
                shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
                    @Override
                    public void onSuccess(Sharer.Result result) {

                        if (ShareDialog.canShow(ShareLinkContent.class)) {
                            ShareLinkContent linkContent = new   ShareLinkContent.Builder()
                                    .setContentTitle("Share on fb")

                                    .setContentUrl(Uri.parse("https://www.google.com"))
                                    .build();

                            shareDialog.show(linkContent);
                        }
                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });

       //     LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email","publish_actions"));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent   data)
{
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

然后您需要创建ShareOpenGraphObject

// Create an object
ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
    .putString("og:type", "books.book")
    .putString("og:title", "A Game of Thrones")
    .putString("og:description", "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.")
    .putString("books:isbn", "0-553-57340-3")
    .build();

然后你将不得不创建行动

ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
    .setActionType("books.reads")
    .putObject("book", object)
    .build();

然后你必须制作内容

 ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
    .setPreviewPropertyName("book")
    .setAction(action)
    .build();

然后

ShareDialog.show(activityOrFragment, content);

注意从Facebook开发者网站复制关注代码。请点击此链接获取更多guidance