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

标签 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

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

编辑 添加了完整的堆栈跟踪

最佳答案

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

profile = gd_client.GetProfile(profile_url)

正在尝试查询位于配置文件中的图片,但如果它不存在就会中断,所以我编辑 try 语句以直接传递配置文件 url:

try:
    gd_client.ChangePhoto(media_object, profile_url)

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

关于python - 更新个人资料图片 Google Apps/Google App Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082261/

相关文章:

python - 无法从 Ubuntu 16.04 卸载 anaconda

python - Django 中如何统计外键属性值

python - 从满足特定条件的 2D numpy 数组返回元素

java - 如何创建简单的 post/get API,使用 Google App Engine 简单调用 GAE 数据库?

python - GAE 错误 204 - 这是什么意思我该如何避免它?

python - NLTK 中是否有任何用于文本规范化和规范化的类?

python - Telethon,如何获得实体?

google-app-engine - 如何使用maven插件下载GAE日志?

python - Google App Engine 不显示任何内容

php - 批量修改谷歌云存储ACL权限