GraphAPIError: (#324) 需要上传文件

发布于 2024-10-18 20:24:24 字数 349 浏览 0 评论 0原文

我正在尝试使用 python sdk 上传图片:

代码:

graph = facebook.GraphAPI(self.current_user.access_token)
graph.put_object("me", "photos", name = "test", message = raw_picture_data)

但收到错误“GraphAPIError:(#324) 需要上传文件”。我不认为这是权限问题,因为我已请求 perms="user_photos,friends_photos,publish_stream"。有谁知道这个错误意味着什么以及如何解决它?

I'm trying to upload a picture with the python sdk:

Code:

graph = facebook.GraphAPI(self.current_user.access_token)
graph.put_object("me", "photos", name = "test", message = raw_picture_data)

But I get the error "GraphAPIError: (#324) Requires upload file". I don't think its a permissions issue as I've requested perms="user_photos,friends_photos,publish_stream". Does anyone know what this error means and how to resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2024-10-25 20:24:24

我使用这个库对图像进行编码: 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 的函数,但它对我不起作用,直到我使用海报对图像进行编码。

希望这有帮助。

I used this library to encode the image: http://atlee.ca/software/poster/

Add this to 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

Call the function with the photo as a file like object:

graph = facebook.GraphAPI(access_token)
photo = open("myphoto.bmp","rb")
graph.put_photo(photo,"me","This is my brilliant photo")

The put_photo method has been submitted by someone (I forget who) as proposed a function to add to the API but it didn't work for me till I used poster to encode the image.

Hope this helps.

在梵高的星空下 2024-10-25 20:24:24

刚刚与类似的错误作了一些斗争。我没有使用 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                             

Just battled with a similar error a bit. I did not use the SDK but just a POST to the graphapi. For me, this error happened when i didnt supply a filename to the file upload field in the "form" sent to facebook.
This is my code (poster - 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                             
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文