2

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);
glo
  • 1,408
  • 3
  • 25
  • 54
  • What are your post parameters? – Ming Li Feb 26 '13 at 16:52
  • _postParameters_ is a bundle that contains message, image link, page link etc – glo Feb 27 '13 at 03:54
  • What I mean is, please provide an example of what you're putting in the postParameters (with code), and what it's actually posting to your wall. That way we can look and maybe point out any mistakes. Your current code sample is too terse to truly debug. – Ming Li Feb 27 '13 at 05:55
  • I have edited my question. Can you find what i am doing wrong – glo Feb 27 '13 at 06:10
  • I don't see anything obviously wrong here. What is an example link that you tried to post? – Ming Li Feb 27 '13 at 06:17
  • You can also go to the graph explorer tool https://developers.facebook.com/tools/explorer/ to try out graph requests. – Ming Li Feb 27 '13 at 06:18
  • An example of link is **http://apps.facebook.com/my_game** – glo Feb 27 '13 at 06:26
  • Well, if you posted a link to your app, wouldn't it make sense for your app details to be posted? Try changing the link to something like instagram.com, or google.com – Ming Li Feb 27 '13 at 06:43
  • The link should be where the user is taken on clicking the post. What should be shown in post must be other details i have passed like name, description, image etc – glo Feb 27 '13 at 06:48
  • @Ming Li I found out the problem. Thank you for trying to help – glo Feb 27 '13 at 10:01

2 Answers2

5

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();
    }
});
}
glo
  • 1,408
  • 3
  • 25
  • 54
0

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

Ferdau
  • 1,524
  • 1
  • 12
  • 21