我正在尝试使用python sdk上传图片:
代码:
graph = facebook.GraphAPI(self.current_user.access_token)
graph.put_object("me", "photos", name = "test", message = raw_picture_data)
但我收到错误"GraphAPIError: (#324) Requires upload file"
。我不认为它是权限问题,因为我已经请求perms =“user_photos,friends_photos,publish_stream”。有谁知道这个错误意味着什么以及如何解决它?
答案 0 :(得分:4)
我使用此库对图像进行编码: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的函数但是在我使用海报对图像进行编码之前它对我不起作用。
希望这有帮助。
答案 1 :(得分:0)
也许你可以试试这个。 http://od-eon.com/blogs/tudor/facebook-photo-upload-google-app-engine/
我试图将此用于solved my problem,但我收到了此回复 -
{“error”:{“type”:“OAuthException”,“message”:“在传递访问令牌时必须使用https://”}}
答案 2 :(得分:0)
稍微与类似的错误作斗争。我没有使用SDK,只是对graphapi的POST。对我来说,这个错误发生在我没有提供文件名到发送到Facebook的“表单”中的文件上传字段。 这是我的代码(海报 - http://pypi.python.org/pypi/poster/0.8.1)
from poster.encode import multipart_encode, MultipartParam
url = 'https://graph.facebook.com/me/photos?access_token=%s'%model.facebook_token
file_param = MultipartParam(name = 'source',
filename = 'photo.jpg', #this is crucial!!!
fileobj = blob_reader) #the blob reader is the fileobject for the file (with read() method)
message_param = MultipartParam(name = 'message',
value = 'test message')
datagen, headers = multipart_encode([file_param,
message_param])
from google.appengine.api import urlfetch
result = urlfetch.fetch(url,
payload = ''.join(datagen),
headers = headers,
method = 'POST')
return result.content