facebook python-sdk 发布到墙的附件

1 投票
1 回答
3173 浏览
提问于 2025-04-16 06:01

你好!
我在 Google App Engine 上使用 python-sdk(http://github.com/facebook/python-sdk.git)。
我正在运行“newsfeed”这个示例。
在 facebook.py 文件中,我需要导入 urllib2,
然后把

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

改成

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

现在基本的应用程序可以正常工作了。不过,如果我在 facebookclient.py 文件中把

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

改成

    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  

我会在 http://localhost:8080 上遇到错误:
HTTP 错误 400:错误的请求
而且应用引擎的开发服务器也会报错:
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] 从 URLFetch 请求中剔除了不允许的头部:['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] 从 URLFetch 请求中剔除了不允许的头部:['Content-Length', 'Host']
INFO 2010-10-24 17:21:20,634 dev_appserver.py:3275] "POST /post HTTP/1.1" 200 -

1 个回答

2

通过使用 put_object 来解决了这个问题,而不是使用 post_to_wall:
可以查看 http://developers.facebook.com/docs/reference/api/post 了解如何使用 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'}
                             )

撰写回答