问题
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.
回答1:
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?
来源:https://stackoverflow.com/questions/5555323/insert-image-to-picasa-in-python-google-app-engine-sdk-through-urlfetch