Question

I am trying to post a feed on Facebook wall without opening a dialogue box using Facebook Android SDK. I tried to find a way but couldn't find. Can anyone tell em how to post to wall in background without opening a dialogue.

I tried using the below code

public static void PublishToFeedInBackground()
{
     final Bundle _postParameter = new Bundle();
     _postParameter.putString("name", name);
     _postParameter.putString("link", link);
     _postParameter.putString("picture", link_to_image);
     _postParameter.putString("caption", caption);
     _postParameter.putString("description", description);

     final List<String> PERMISSIONS = Arrays.asList("publish_actions");

     if (Session.getActiveSession() != null)
     {
            // Check for publish permissions    
            List<String> _permissions = Session.getActiveSession().getPermissions();
            if (!isSubsetOf(PERMISSIONS, _permissions))
            {
                NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(this.GetContext(), PERMISSIONS);
                Session.getActiveSession().requestNewReadPermissions(reauthRequest);
                return;
            }   
     }

    this.runOnUiThread(new Runnable()
    {
        @Override
        public void run() 
        {
            Request request = new Request(Session.getActiveSession(), "me/feed", _postParameter, HttpMethod.POST);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }
    });
}

But it posts my app page details instead of the *_postParameter* i give.

I also tried to use 2 other methods but it didn't post anything

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", name);
    params.put("link", link);
    params.put("picture", link_to_image);
    params.put("caption", caption);
    params.put("description", description);

    JSONObject jfeed = new JSONObject(params);
    final GraphObject _feed = GraphObject.Factory.create(jfeed);

    Request.executePostRequestAsync(Session.getActiveSession(), "https://graph.facebook.com/"+userID+"/feed", _feed, new Request.Callback()
    {
       @Override
       public void onCompleted(Response response) 
       {
            Log.i("tag", response.toString());
       }
   });

Second method is

Request.executeRestRequestAsync(Session.getActiveSession(), "stream.publish", _postParameter, HttpMethod.POST);
Était-ce utile?

La solution

I found out what the problem was. I was not asking the right permissions and the graph path was also wrong. The right method is :

public static void PublishToFeedInBackground()
{

final Bundle _postParameter = new Bundle();
 _postParameter.putString("name", name);
 _postParameter.putString("link", link);
 _postParameter.putString("picture", link_to_image);
 _postParameter.putString("caption", caption);
 _postParameter.putString("description", description);

     final List<String> PERMISSIONS = Arrays.asList("publish_stream");

 if (Session.getActiveSession() != null)
 {
       NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(this.GetContext(), PERMISSIONS);
        Session.getActiveSession().requestNewPublishPermissions(reauthRequest);
 }

this.runOnUiThread(new Runnable()
{
    @Override
    public void run() 
    {
        Request request = new Request(Session.getActiveSession(), "feed", _postParameter, HttpMethod.POST);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }
});
}

Autres conseils

As far as I know, this was possible with the REST api which is now deprecated... https://developers.facebook.com/docs/reference/rest/

EDIT: here are the functions you can use to post https://developers.facebook.com/docs/reference/rest/#publishing-methods

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top