更新个人资料图片Google Apps / Google App Engine

时间:2012-01-31 15:36:06

标签: python google-app-engine

我正在Google App Engine上编写一个小应用来更新我们用户个人资料中的图片。它需要用户名和图像,然后放置到该配置文件,上传图像。这就是我所拥有的:

import atom.data
import gdata.data
import gdata.contacts.client
import gdata.contacts.data
import cgi
import wsgiref.handlers

from google.appengine.api import users
from google.appengine.ext import webapp

email = 'admin@domain.com'
password = 'password'
domain = 'domain.com'

gd_client = gdata.contacts.client.ContactsClient(domain=domain)
gd_client.ClientLogin(email, password, 'photoUpdate')


class PicPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""<html><head><title>Sasaki Photo Uploader</title>
                                    <link type="text/css" rel="stylesheet" href="/stylesheets/form.css"></head>
                                    <body>
                                    <form action="/" enctype="multipart/form-data" method="post">
                                    <div><label>Person Name</label></div>
                                    <div><textarea name="name" rows="2" columns "60"></textarea></div>
                                    <div><label>Image</label></div>
                                    <div><input type="file" name="img"/></div>
                                    <div><input type="submit" value="Upload" /></div>
                                    </form>
                                    </body>
                                    </html>""")

    def post(self):
        person_name = self.request.get('name')
        img_img = self.request.get('img')
        profile_url = 'https://www.google.com/m8/feeds/photos/profile/domain.com/%s' % person_name
        media_object = img_img
        print(profile_url)
        profile = gd_client.GetProfile(profile_url)
        print(profile)
        gd_client.ChangePhoto(media_object, profile)
        self.redirect('/')

def main():
  application = webapp.WSGIApplication(
                                       [('/', PicPage)
                                        ],
                                       debug=True)

  wsgiref.handlers.CGIHandler().run(application)

if __name__=="__main__":
  main()

当我运行它时,它返回错误:

 Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "C:\GAE_Local_Files\picupload\sasakipic.py", line 40, in post
    profile = gd_client.GetProfile(profile_url)
  File "C:\GAE_Local_Files\picupload\gdata\contacts\client.py", line 375, in get_profile
    auth_token=auth_token, **kwargs)
  File "C:\GAE_Local_Files\picupload\gdata\client.py", line 652, in get_entry
    desired_class=desired_class, **kwargs)
  File "C:\GAE_Local_Files\picupload\gdata\client.py", line 278, in request
    version=get_xml_version(self.api_version))
  File "C:\GAE_Local_Files\picupload\atom\core.py", line 520, in parse
    tree = ElementTree.fromstring(xml_string)
  File "<string>", line 106, in XML
ParseError: not well-formed (invalid token): line 1, column 0

我不确定这是不是因为我将个人资料网址作为字符串传递或上传图片错误。任何建议,非常感谢。

编辑添加完整堆栈跟踪

3 个答案:

答案 0 :(得分:1)

嗯,答案在于我在编辑中指出的问题。那一行:

profile = gd_client.GetProfile(profile_url)

正在尝试查询位于个人资料中的图片,但如果它不存在则会中断,所以我编辑try语句直接传递个人资料网址:

try:
    gd_client.ChangePhoto(media_object, profile_url)

效果很好。这是使用App Engine更新图片的一个很棒的小工具,你甚至不必上传它,只需在app引擎测试服务器上本地运行它。我想尝试添加一些功能,比如图像裁剪,或者在上传之前调整大小,大图像在联系人资料页面上看起来很好,但是在聊天图像中可能会失真。

答案 1 :(得分:1)

我尝试使用您在上面发布的脚本,包括您已突出显示的修复程序,以修复无效令牌错误,但对我来说却出现了另一个错误:

UnknownSize: Each part of the body must have a known size.

为了纠正这个问题,我修改了一行

media_object = img_img

阅读:

media_object = gdata.data.MediaSource(img_img)

因为MediaSource对象具有隐式大小并且是可接受的client.py参数之一 - 除了现在出现新的错误并且我不知道要解决什么。

RequestError: Server responded with: 400, Malformed Content-Type

我尝试在ChangePhoto中添加 content-type ='image / *'参数,但没有任何影响。

有什么想法吗?

错误信息流:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File " ...trim... /main.py", line 126, in post
    gd_client.ChangePhoto(media_object, profile_url, content_type='image/*')
  File " ...trim... /gdata/contacts/client.py", line 288, in change_photo
    ifmatch_header=ifmatch_header, **kwargs)
  File " ...trim... /atom/client.py", line 142, in put
    http_request=http_request, data=data, **kwargs)
  File " ...trim... /gdata/client.py", line 319, in request
    RequestError)
RequestError: Server responded with: 400, Malformed Content-Type

答案 2 :(得分:0)

我认为问题是你需要验证对API的请求。见这里:

http://code.google.com/apis/contacts/docs/3.0/developers_guide.html#Auth

并查看此示例应用程序了解oauth流的工作原理:

http://code.google.com/p/google-api-python-client/source/browse/samples/appengine/main.py

您可能还有第二个问题需要指定文件的内容类型,但第一步是验证以获取配置文件。