31Aug

How to post to a facebook user’s Newsfeed via Python and Django

Posted by Elf Sternberg as django, javascript, python

The correct call for posting to a user’s facebook wall with Python and pyfacebook, after you’ve established both user authentication via FacebookConnect and gotten stream_publish permission, is:

request.facebook.stream.publish(
    message = render_to_string(template_path, fb_context),
    action_links = simplejson.dumps(
        [{'text': "Check Us Out!", 'href': "http://someurl.com"}]),
    target_id = 'nf')

See that ‘nf’ down there in target_id?  It’s not on any of the Facebook documentation pages, but that is the correct string to post to your user’s facebook Newsfeed. (For that matter, the fact that you have to run the action_links through simplejson, and that they have to match the PublishUserAction API action_links spec, is also not documented; the documentation says it just needs an array of arrays.)  I have no idea how to post to some other user’s newfeed, but at least I’m one step closer.

Oh, another important tip: in order to make my news “stories” consistent, I’m using a template to post them to Facebook.  The template must not have newlines within, or they will show up on Facebook and it’ll look all ugly.  Every paragraph should be one long line of text without line breaks.

8 Responses to How to post to a facebook user’s Newsfeed via Python and Django

wucj

September 10th, 2009 at 1:43 am

Hi
i intent publish some message with photo from my application to facebook wall and My application is django framework. But, i have failed so far. It’ my code
fb = facebook.Facebook(settings.FACEBOOK_API_KEY, settings.FACEBOOK_SECRET_KEY)
picinfo = simplejson.dumps([{‘name':’lalala’, ‘href':’http://myurl.com/’, ‘caption':’cccc’, ‘description':’ddd’, ‘media':{‘type':’image’, ‘src':’http://myurl.com/staticmap/user/5157d6da.jpg’, ‘href':’http://myurl.com/trail/5157d6da/’}}])
fb.stream.publish(message=’test message with pic’, attachment=picinfo, uid=user.uid)

just the message is shown on my wall, i can’t see photo and link
Would you tell me what’s wrong

Elf Sternberg

September 10th, 2009 at 8:42 am

Your media type is odd. I would have done this:

fb.stream.publish(
    message = "Test Message with Picture",
    attachment = simplejson.dumps({ 'media': [
                {'type': 'image',
                 'src': 'http://myurl.com/staticmap/user/5157d6da.jpg',
                 'href': http://myurl.com/trail/5157d6da/'}]}),
    action_links = simplejson.dumps([{'text': lalala', 'href': 'http://myurl.com/'}]),
    uid = user.uid) 

wucj

September 10th, 2009 at 9:05 pm

Hi
Thanks a lot for your sample. I have tested it and put photo to my facebook wall successfully.

mp

September 18th, 2009 at 3:24 pm

I tried using stream.publish but I always get an attribute error.

AttributeError: ‘Facebook’ object has no attribute ‘stream’

I am using FBML.
Thanks

Tomas

October 13th, 2009 at 1:42 pm

Hi MP, I had the same problem and it was because of old veriosn of my pyfacebook library. Try to download it from GIT and get the newest version. It worked for me!

Fahim Akhter

December 18th, 2009 at 4:57 am

Hi,

I tried the above given code as the solution. Well for starters it gives me a syntax error and if I take all parameters out I get “Facebook has no attribute stream”

Still stuck on publishing the stream sadly.

Fahim

jori

April 14th, 2010 at 10:00 pm

I was wondering how you can get the publish stream access? Any advices?

Israel

February 1st, 2011 at 12:22 pm

Hello:

I have more problems understand the libraries to put in my page information via python.

I donwload pyfacebook but I dont know how I realease, I have a API KEY and SECRET KEY.

If you dont mind send me a long example that works in pyhton 2.7

Thanks for all and sorry my bad english

Comment Form

Subscribe to Feed

Categories

Calendar

August 2009
M T W T F S S
« Jul   Sep »
 12
3456789
10111213141516
17181920212223
24252627282930
31