Facebook共享对话框PostId为空

时间:2015-08-07 15:49:20

标签: android facebook facebook-sharer

我通过Android中的分享对话框在FB上分享视频。分享完美无缺。但是,FB post id返回null。即使在上传视频之前,回调也会返回。如果我遗漏了什么,请告诉我。以下是我的代码。

public class TestFragment extends Fragment {

    private CallbackManager callbackManager;
    private ShareDialog shareDialog;

    public TestFragment() {
        // Required empty public constructor
    }

    public static TestFragment newInstance(String path, String json) {
        TestFragment fragment = new TestFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getActivity());
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog(this);
        // this part is optional
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                Timber.d("result.getPostId() :: " + result.getPostId());
            }

            @Override
            public void onCancel() {
                Timber.d("Facebook : Cancelled");
            }

            @Override
            public void onError(FacebookException e) {
                Timber.d(e.getMessage());
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @OnClick(R.id.facebookShare)
    public void share() {
        Timber.d("share button pressed");
        if (ShareDialog.canShow(ShareVideoContent.class)) {
            Timber.d("showing share dialog");
            shareDialog.show(getVideoContent());
        } else {
            Timber.d("unable to show the share dialog");
        }
    }

    private ShareVideoContent getVideoContent() {
        Timber.d(mVideoMetadata.getVideoId());

        ShareVideo shareVideo = new ShareVideo.Builder()
                .setLocalUrl(Uri.parse("... file ..."))
                .build();
        ShareVideoContent content = new ShareVideoContent.Builder()
                .setVideo(shareVideo)
                .build();

        return content;
    }

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

2 个答案:

答案 0 :(得分:0)

shareDialog.show(shareContent,ShareDialog.Mode.FEED);

将模式设置为ShareDialog.Mode.FEED。
它对我有用。

Here's example

答案 1 :(得分:0)

使用Facebook登录成功后再调用此方法 shareOnFacebook()

private void shareOnFacebook(){

    ShareLinkContent shareContent = new ShareLinkContent.Builder()
            .setContentTitle("The Simpson!")
            .setContentUrl(Uri.parse("http://www.codecube.in/airbucks-project"))
            .build();

    mFacebookShareDialog.show(shareContent, ShareDialog.Mode.FEED);

    mFacebookShareDialog.registerCallback( this.callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            Log.v("MyApp", "Share success!"); //Showed if I press the share or the cancel button
            String postID = result.getPostId();
            Log.v("MyApp", "Share success!" +result.getPostId());                        
        }
        @Override
        public void onCancel() {
            Log.v("MyApp", "Share canceled"); //Only showed when I press the close button
        }

        @Override
        public void onError(FacebookException e) {
            Log.v("MyApp","Share error: " + e.toString());
        }    
    });
    mFacebookShareDialog.show(shareContent, ShareDialog.Mode.FEED);
}


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