简体   繁体   English

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

[英]Update Profile Picture Google Apps / Google App Engine

I am writing a small app on Google App Engine to update the pictures in our users profiles. 我正在Google App Engine上编写一个小应用程序来更新用户个人资料中的图片。 It takes there username and an image and does a put to there profile, uploading the image. 它需要用户名和图像,然后放置到该配置文件,上传图像。 Here is what i have: 这是我有的:

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

When I run this it returns the error: 当我运行它时,它返回错误:

 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

i am not sure if this is because I am passing the profile url off as a string or uploading the pic wrong. 我不确定这是不是因为我将个人资料网址作为字符串传递或上传图片错误。 Any advice, much appreciated. 任何建议,非常感谢。

EDIT Added full stack trace 编辑添加了完整的堆栈跟踪

Well, the answer lies in the issue that I pointed out in my edit. 嗯,答案在于我在编辑中指出的问题。 That line: 那条线:

profile = gd_client.GetProfile(profile_url)

Is trying to query the picture located in the profile, but if it doesn't exist it breaks, so I edit the try statement to pass the profile url directly: 试图查询位于配置文件中的图片,但如果它不存在则会中断,所以我编辑try语句直接传递配置文件URL:

try:
    gd_client.ChangePhoto(media_object, profile_url)

It worked great. 它运作得很好。 This is a great little tool to update picture using App Engine, and you don't even have to upload it, just run it locally on a app engine test server. 这是使用App Engine更新图片的一个很棒的小工具,你甚至不必上传它,只需在app引擎测试服务器上本地运行它。 I want to try and add some functionality, like image cropping, or resizing before the upload, large images look fine on the contact profile page, but can get distorted in the chat image. 我想尝试添加一些功能,如图像裁剪,或在上传之前调整大小,大图像在联系人资料页面上看起来很好,但可能会在聊天图像中失真。

I have tried to use the script you posted above including the fix you've highlighted as a fix to the invalid token error, but for me that sprang a different error: 我曾尝试使用您在上面发布的脚本,包括您已经突出显示的修复程序,以修复无效令牌错误,但对于我而言,出现了另一个错误:

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

To rectify this I amended the line 为了纠正这个问题,我修改了这条线

media_object = img_img

to read: 读书:

media_object = gdata.data.MediaSource(img_img)

because the MediaSource object has implicit size and is one of accepted client.py parameters – except that a new error now springs up and i'm at a loss to know what to address. 因为MediaSource对象具有隐式大小并且是接受的client.py参数之一 - 除了现在出现新的错误并且我不知道要解决什么。

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

i have tried adding a content-type='image/*' parameter to the ChangePhoto but it has no affect. 我已经尝试将一个content-type ='image / *'参数添加到ChangePhoto,但它没有任何影响。

Any ideas? 有任何想法吗?

error stream: 错误流:

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

I believe the problem is you need to authenticate the request to the API. 我认为问题是你需要验证对API的请求。 See here: 看这里:

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

and look at this sample app for how the oauth flow works: 并查看此示例应用程序以了解oauth流的工作原理:

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

You might have a second problem that you need to specify the content-type of the file, but the first step is authenticating to get the profile. 您可能有第二个问题需要指定文件的内容类型,但第一步是验证以获取配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM