Need to create a valid session using the API class.

Jun 9, 2009 at 1:57 PM

I am writing an application that is supposed to push updates to a user's facebook wall/profile/etc. The user logs onto facebook, adds the app, and gives it permission to publish to her profile. That part works fine. The app runs as a Windows service, so it has nothing to do directly with the user's session, cookies etc etc.


My code looks something like :

API api = new API();

api.ApplicationKey = "myappkey";

api.Secret = "mysecret";


// do all template / story building etc



// and this is where it dies

api.feed.publishUserAction(bundleID, templateData, ids, "Test story.", facebook.feed.PublishedStorySize.Short);


I get an "A session key is required for calling this method" error. Calling api.ConnectToFacebook() doesnt generate a session. I have seen suggestions elsewhere that one should derive from the IFrame or FBML form or windows controls and let those handle the handshake. In my situation that's not possible because I am using neither, and I really need to manage all authentication myself using just the API class. For obvious reasons using Facebook connect is completely out of the question, as this process must run autonomously after the user has given it consent to posting.

I also want to point out that with the instance of the API shown above, I can defintely connect to Facebook in "sessionless" mode, and easily request user info etc, so my app key and secret work fine. Things go wrong only when I try to do things which require a session.


Many thanks





Jun 9, 2009 at 5:27 PM

If your app is setup as desktop app, you'll need to login to it as yourself and establish a session on behalf of your user.  You can then set the SessionKey in the API directly.  If you look at the unit tests, you'll see that in the cases where I need a session, I have one that is valid for the user and app hardcoded (which I captured by logging into the app one time).  I then set the SessionKey directly on the Api instance when I create it.

The real question is what are you trying to publish.  Because this method is used for publishing to the logged in user.  So, following my advice above, you would always be publishing actions for your own profile.  So, I'm not sure what you are trying to do in a service that would call this method.  But, this method is definitely part of the Session Required API.


Jun 9, 2009 at 5:48 PM

The application that I have in mind would be one that publishes event notifications from another website, onto a user's Facebook activity feed.. This is not an application that the user herself uses within Facebook - it would be one that runs entirely independantly of the user, and "pushes" notifications onto her profile, like the Flickr or RSS notifier applets on Facebook ............... after what you've just said I find myself wondering all of a sudden - is what I'm trying to do even possible on Facebook? It is with APIs like Flickr's, where you can write an app that creates content on a user's profile without having to log on as the user.


- Shukri


Jun 9, 2009 at 5:55 PM

its possible to publish to that activity feed without a session using stream.publish.  This one is session optional.  you'll need version 2.1 to do that.  The only part that I am not sure how you plan to do is you'll need the users facebook uid...



Jun 10, 2009 at 7:43 AM
Edited Jun 10, 2009 at 9:07 AM

Oh, thanks! That's what I'm looking for. User id is captured in another process, so that part should be covered. Thanks again!

Edit : and it worked. Just published to my wall using only a userid. Thanks again for the help!


- Shukri

Sep 17, 2009 at 1:39 AM

I am developing an application that has a very similar flow.  When I attempt to publish to the users stream I get a permission denied error and it says the user has to give my application permissions to publish to their stream.  Is this something you encountered as well?  The applicaiton I am developing doens't run inside of facebook and it basically pushes messages to users.  I have just created a test fixture for now but here is the code: 

var friends = _fbService.friends.getUserObjects();
foreach (facebook.Schema.user friend in friends)
  attachment attachment = new attachment();

  attachment.caption = ""; = "Radar Facebook test";
  attachment.href = "";
  attachment.description = "Radar's first facebook push"; = new attachment_property()
   category = new attachment_category()
         href = "",
                text = "Radar"
        ratings = "5 stars"
      = new List<attachment_media>(){new attachment_media_image()
   src = "",
        href = ""
  }};"This is a test message from Radar", attachment, null, friend.uid.ToString(), Convert.ToInt32(friend.uid));

Sep 17, 2009 at 2:02 AM

You need to ask for the permission.

If you use the 3.0 source, you can pass in the required permission to the desktop session and it will prompt the user for the permissions that you need.  Look up extended permissions on the wiki for more info.