Jon G
Jon G

Reputation: 1706

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

I want to be able to seamlessly publish to a users Facebook feed from the background in my Android application. The user is fully aware of the content they are publishing, but the post will be made after a relatively long upload process, so the user should not be made to wait. I am using the Android Facebook SDK v4.1.2.

I see that I need to request the "publish_actions" permission from the user beforehand:

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

I can use the LoginManager to get the access token:

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

I am then not sure how to share a link to the users feed without using the ShareDialog:

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

This SO question suggests using the Request object for v3.x, which is equivalent to the GraphRequest object for v4.x:

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

but I am not sure how to build the correct GraphRequest. I assume I need to publish a message to "/me/feed":

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

Can anyone tell me how to do this?

Upvotes: 0

Views: 1368

Answers (2)

Ming Li
Ming Li

Reputation: 15662

You should use the ShareApi class if you don't want to build the params bundle yourself.

Build a ShareLinkContent as you normally would:

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

Then call ShareApi:

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

Upvotes: 1

Jon G
Jon G

Reputation: 1706

In writing the question I found my own solution from trawling the docs and examples. I need to publish a message to "/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();

After obtaining the "publish_actions" permission.

Upvotes: 2

Related Questions