GraphPierror:(#324)需要上载fi

2024-09-26 17:44:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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=“用户照片,朋友照片,发布流”。有人知道这个错误意味着什么,以及如何解决它吗?在


Tags: 代码selftokenfacebookobjectaccessput错误
3条回答

我用这个库来编码图像:http://atlee.ca/software/poster/

将此添加到脸谱网.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

将照片作为类似文件的对象调用函数:

^{pr2}$

put_photo方法已经由某人(我忘了是谁)提交的,建议添加到API中,但直到我使用poster对图像进行编码,它才对我起作用。在

希望这有帮助。在

也许你可以试试这个。 http://od-eon.com/blogs/tudor/facebook-photo-upload-google-app-engine/

我试着用这个来solved my problem,但是我得到了这个响应-
{“error”:{“type”:“OAuthException”,“message”:“在传递访问令牌时必须使用https://”}

只是和一个类似的错误作斗争。我没有使用SDK,只是在graphapi上发布了一个帖子。对我来说,这个错误发生在我没有给发送到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                             

相关问题 更多 >

    热门问题