通过urlfetch将图像插入Python

通过urlfetch将图像插入Python

本文介绍了通过urlfetch将图像插入Python Google App Engine Sdk中的Picasa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过Google App Engine Sdk将Flex应用程序中的图像插入到picasa网站。我想做一个简单的urlfetch而不是python客户端库。我的代码如下:

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
                                })

传递给filename_or_handle的数据是ByteArray图像。但是,它没有成功。我不知道问题是什么。请指教。谢谢。

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.

解决方案如下:

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
                })

谢谢,约翰逊。

推荐答案

Payload应该是一个字符串,而不是类文件对象。它应该如何编码取决于您正在调用的API - 请参阅API的文档以确定它,以及您需要设置的任何其他标头等。您不太可能需要对其进行base64编码 - 只需直接传递图像内容。

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?

这篇关于通过urlfetch将图像插入Python Google App Engine Sdk中的Picasa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:16