我用這個庫來對圖像進行編碼: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