从后台分享到 Facebook - Android - Facebook SDK v4.x

Share to facebook from background - Android - Facebook SDK v4.x

我希望能够在我的 Android 应用程序中从后台向用户无缝发布 Facebook 提要。用户对自己发布的内容是完全清楚的,但是post是经过一个比较长的上传过程,所以不应该让用户等待。我正在使用 Android Facebook SDK v4.1.2。

我看到我需要事先请求用户的 "publish_actions" 许可:

https://developers.facebook.com/docs/facebook-login/permissions/v2.3#reference-publish_actions

我可以使用 LoginManager 获取访问令牌:

https://developers.facebook.com/docs/facebook-login/android/v2.3#access_profile

然后我不确定如何在不使用 ShareDialog 的情况下与用户分享 link:

https://developers.facebook.com/docs/sharing/android

这个SO question建议使用v3.x的Request对象,相当于v4.x的GraphRequest对象:

https://developers.facebook.com/docs/reference/android/current/class/GraphRequest/

但我不确定如何构建正确的 GraphRequest。我假设我需要向 "/me/feed":

发布消息

https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed

谁能告诉我怎么做?

在编写问题时,我通过搜索文档和示例找到了自己的解决方案。我需要向 "/me/feed":

发布消息

https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed

Bundle params = new Bundle();
params.putString("link", link);

GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(),
    "/me/feed", params, HttpMethod.POST,
    new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {

        }
    });
request.executeAndWait();

获得"publish_actions"权限后

如果您不想自己构建参数包,您应该使用 ShareApi class。

像往常一样构建 ShareLinkContent:

ShareLinkContent content = new ShareLinkContent.Builder()
    .setContentUrl(Uri.parse("https://your.url.com"))
    .build();

然后调用ShareApi:

ShareApi.share(content, new FacebookCallback<Sharer.Result>() {...});