开发者

Insert image to Picasa in Python Google App Engine Sdk through urlfetch

开发者 https://www.devze.com 2023-02-22 11:31 出处:网络
I try to insert an image from a Flex application to picasa web through Google App Engine Sdk.I want to do a simple urlfetch instead of the python 开发者_开发百科client library. The code i following:

I try to insert an image from a Flex application to picasa web through Google App Engine Sdk. I want to do a simple urlfetch instead of the python 开发者_开发百科client library. The code i following:

    def Insert(self, sessionToken, album_or_uri, title, filename_or_handle):
        result = urlfetch.fetch(url=album_or_uri,
                                method=urlfetch.POST,
                                follow_redirects=True,
                                payload=StringIO(filename_or_handle),
                                headers={'Authorization': 'AuthSub token="' + sessionToken + '"',
                                'Content-Length': str(len(filename_or_handle)),
                                'Content-Type': 'image/jpeg',
                                'Slug': title
                                })

The data pass to "filename_or_handle" is a ByteArray image. However, it is not successful. I have no idea what the problem is. Please advice. Thanks.

The solution is following:

def Insert(self, sessionToken, album_or_uri, title, filename_or_handle):
    image = filename_or_handle.read()
    contentLength = len(image)
    result = urlfetch.fetch(url=album_or_uri,
                method=urlfetch.POST,
                follow_redirects=True,
                payload=image,
                headers={'Authorization': 'AuthSub token="' + sessionToken + '"',
                'Content-Length': contentLength,
                'Content-Type': 'image/jpeg',
                'Slug': title
                })

Thanks, Johnson.


Payload should be a string, not a file-like object. How it should be encoded depends on the API you're calling - see the docs for the API to determine that, and any other headers etc you need to set. It's unlikely you need to base64 encode it - just pass the contents of the image in directly.

If you're still having problems, you'll have to be more specific than "it is not successful" - what response do you get?

0

精彩评论

暂无评论...
验证码 换一张
取 消