2012-01-31 124 views
2

我正在Google App Engine上编写一个小应用程序,以更新用户个人资料中的图片。它需要那里的用户名和图像,并把它放到那里配置文件,上传图像。这里是我有:如果这是因为我传递的个人资料网址了作为一个字符串或上传更新个人档案图片Google Apps/Google App Engine

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 

我不知道:

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 = '[email protected]' 
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() 

当我运行这个它返回错误图错了。任何建议,非常感谢。

编辑添加完整的堆栈跟踪

+0

什么是错误的完整调用堆栈?我怀疑你需要在调用gd_client.ChangePhoto时使用MediaSource对象。 – 2012-01-31 16:33:46

+0

您可以在picupload \ atom \ core.py:520中放置断点(或打印语句)以检查xml_string的值 – proppy 2012-02-08 13:19:57

回答

0

那么,答案就在于我在编辑中指出的问题。该行:

profile = gd_client.GetProfile(profile_url) 

试图查询位于配置文件中的图片,但如果它不存在,它打破了,所以我编辑try语句通过直接的个人资料网址:

try: 
    gd_client.ChangePhoto(media_object, profile_url) 

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

-1

我相信问题是你需要验证请求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

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

+0

客户端登录位于每个gdata示例的顶部,因此我不确定是否是问题所在。在堆栈跟踪中,在我将文本从框中传递到URL并且最后一次调用到XML解析器的行处失败。感谢您的输入。 – Kevin 2012-02-01 18:31:56

+0

我敢打赌,XML解析器试图解析错误返回。 – 2012-02-01 19:42:02

+0

我没有使用联系人api,我正在使用google应用程序配置文件gdata库。在第一个链接中,它明确表示这是用于Google帐户,而不是Google Apps,它使用admin clientLogin来处理PROFILE,而不是联系人编辑。 – Kevin 2012-02-03 16:55:49

0

我曾尝试使用您发布上面包括您所标明的修复程序无效的令牌错误修复程序的脚本,但对我来说窜出一个不同的错误:

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但它没有影响。

任何想法?

错误流:

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