facebook python-sdk post_to_wall attachment

2020-07-31 06:26发布

问题:

Hi I am using the python-sdk (http://github.com/facebook/python-sdk.git) on google appengine. I am runnig the "newsfeed" example.
In facebook.py i had to import urllib2
and then change

file = urllib.urlopen("https://graph.facebook.com/" + path + "?" +
                          urllib.urlencode(args), post_data)  

to

file = urllib2.urlopen("https://graph.facebook.com/" + path + "?" +
                          urllib.urlencode(args), post_data)  

Now the basic application works. However if I change
in facebookclient.py

    try:  
        self.graph.put_wall_post(message)  
    except Exception, e:  
        self.response.out.write(e)  
        return  

to

    try:  
        attachment = {}  
        message = message         
        caption = 'test caption'  
        attachment['caption'] = caption  
        attachment['name'] = 'test name'  
        attachment['description'] = 'test description'  
        self.graph.put_wall_post(message, attachment=attachment)  
    except Exception, e:  
        self.response.out.write(e)  
        return  

i will get the error (on http://localhost:8080) :
HTTP Error 400: Bad Request
and the appengine development server complains:
INFO 2010-10-24 17:20:44,483 dev_appserver.py:3275] "POST /post HTTP/1.1" 302 - WARNING 2010-10-24 17:20:44,570 urlfetch_stub.py:284] Stripped prohibited headers from URLFetch request: ['Host']
INFO 2010-10-24 17:20:48,167 dev_appserver.py:3275] "GET / HTTP/1.1" 200 -
INFO 2010-10-24 17:20:48,292 dev_appserver.py:3275] "GET /static/base.css HTTP/1.1" 200 -
WARNING 2010-10-24 17:21:19,343 urlfetch_stub.py:284] Stripped prohibited headers from URLFetch request: ['Content-Length', 'Host']
INFO 2010-10-24 17:21:20,634 dev_appserver.py:3275] "POST /post HTTP/1.1" 200 -

回答1:

Solved the problem by using put_object instead of post_to_wall:
see http://developers.facebook.com/docs/reference/api/post for an example on how to post with curl

 self.graph.put_object("me", "feed", message=message,  
                             link="http://leona-nachhilfe.appspot.com",  
                             picture="http://leona-nachhilfe.appspot.com/static/images/logo.png",    
                             name = "LeONa-Quiz",    
                             description = "Orges erreichte 45.Punkte",
                             actions = {'name': 'Zu den Quiz-Aufgaben', 'link': 'http://leona-nachhilfe.appspot.com'},
                             privacy = {'value': 'ALL_FRIENDS'}
                             )