我用这个库来对图像进行编码:http://atlee.ca/software/poster/
添加这facebook.py:
from poster.encode import *
from poster.streaminghttp import register_openers
def put_photo(self, source, album_id=None, message=""):
object_id = album_id or "me"
register_openers()
content_type,body = multipart_encode([ ('message',message),('access_token',self.access_token),('source',source) ])
req = urllib2.Request("https://graph.facebook.com/%s/photos" % object_id, content_type,body)
try:
data = urllib2.urlopen(req).read()
except urllib2.HTTPError as e:
data = e.read()
try:
response = _parse_json(data)
if response.get("error"):
raise GraphAPIError(response["error"].get("code", 1),response["error"]["message"])
except ValueError:
response = data
return response
电话与照片的功能文件中像对象:
graph = facebook.GraphAPI(access_token)
photo = open("myphoto.bmp","rb")
graph.put_photo(photo,"me","This is my brilliant photo")
的put_photo方法已提交有人(我忘记是谁)所提出的功能添加到API,但直到我用海报来对图像进行编码它没有为我工作。
希望这会有所帮助。
我得到这个错误{ “错误”:{ “类型”: “OAuthException”, “消息”: “(#324)需要上传文件”}} – Adham 2011-03-05 16:32:32