【问题标题】:Share to facebook from background - Android - Facebook SDK v4.x从后台分享到 facebook - Android - Facebook SDK v4.x
【发布时间】:2015-05-27 15:53:56
【问题描述】:

我希望能够在我的 Android 应用程序的后台无缝地向用户发布 Facebook 提要。用户完全了解他们正在发布的内容,但该帖子将在相对较长的上传过程之后发布,因此不应让用户等待。我正在使用 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 的情况下共享指向用户供稿的链接:

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

谁能告诉我怎么做?

【问题讨论】:

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


    【解决方案1】:

    在编写问题时,我通过搜索文档和示例找到了自己的解决方案。我需要发消息给"/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"权限后。

    【讨论】:

      【解决方案2】:

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

      像往常一样构建 ShareLinkContent:

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

      然后调用ShareApi:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多