OAuth + Aeoid + Python + Google App Engine + Google文档

5
我正在尝试在Google App Engine上完成一个学校报纸的故事任务系统。 它将跟踪作者的截止日期,允许作者选择故事,并提供本周故事的“一目了然”的视图。 我和我的合作伙伴正在努力将其与我们的报纸Google Apps安装完全集成。哦,而且我们必须使用三条腿的Oauth,因为我们没有Google Apps Premier。
在这个过程中,我偶然发现了Aeoid并能够按照说明使联合登录工作。 这很酷!
我遇到问题的地方是使用Oauth获取用户的谷歌文档列表。 我已经设置了一个测试页面:mustrun.cornellsun.com/test。 它给我带来了错误-我已将它们复制在此邮件的底部。 我不知道这是否与我的消费者密钥有关(我应该使用从google市场获得的密钥吗?还是应该使用从管理域页面获得的密钥?)。 目前,我正在使用从管理域页面获得的密钥
此外,实际的appspot域是mustrun2sun [].appspot[太新了,无法发布超过一个链接]。com,但我在google apps中设置了它,以便只有来自我的域的用户可以登录,并且还设置了应用程序在我的域上部署。 (应用程序部署为must[]run[].corn[]ellsun[].[]com,所有内容都以此为参考,即使在管理域中也是如此。)
我正在使用GDClient 2.0类,因此我相当确定一切都应该按计划进行...即我没有使用旧的服务等任何东西。 我已经使用htt[]p:/[]/k[]ing[]yo-bachi.blog[]spot.c[]om/2010/05/gaego[]ogleoauth.ht[]ml作为我的Oauth“舞蹈”的模板,因为Google的示例已过时并使用旧的Google data 1.0库-我想。
当我转到我的测试页面时,我收到的错误是:
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 511, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/mustrun2sun/1.341947133742569880/main.py", line 170, in get
    feed = client.GetDocList(auth_token=gdata.gauth.AeLoad(users.get_current_user().user_id())) #auth_token=TOKEN
  File "/base/data/home/apps/mustrun2sun/1.341947133742569880/gdata/docs/client.py", line 141, in get_doclist
    auth_token=auth_token, **kwargs)
  File "/base/data/home/apps/mustrun2sun/1.341947133742569880/gdata/client.py", line 635, in get_feed
    **kwargs)
  File "/base/data/home/apps/mustrun2sun/1.341947133742569880/gdata/client.py", line 308, in request
    response, Unauthorized)
Unauthorized: Unauthorized - Server responded with: 401, <HTML>
<HEAD>
<TITLE>Token invalid - Invalid AuthSub token.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Token invalid - Invalid AuthSub token.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

此外,由于没有任何源代码,因此很难实现。以下是相关代码:

import gdata.auth
import gdata.gauth
import gdata.docs.client
import gdata.docs.data
import gdata.docs.service
import gdata.alt.appengine

from aeoid import middleware, users

class GetOauthToken(webapp.RequestHandler):
    def get(self):
        user_id = users.get_current_user().user_id()
        saved_request_token = gdata.gauth.AeLoad("tmp_"+user_id)
        gdata.gauth.AeDelete ("tmp_" + user_id)
        request_token = gdata.gauth.AuthorizeRequestToken(saved_request_token, self.request.uri)
        #upgrade the token
        access_token = client.GetAccessToken(request_token)
        #save the upgraded token
        gdata.gauth.AeSave(access_token, user_id)
        self.redirect('/test')     

class Test(webapp.RequestHandler):
    def get(self):
        TOKEN = gdata.gauth.AeLoad(users.get_current_user().user_id())
        if TOKEN:
            client = gdata.docs.client.DocsClient(source=SETTINGS['APP_NAME'])
            client.auth_token = gdata.gauth.AeLoad(users.get_current_user().user_id()) #could try to put back as TOKEN?

            self.response.out.write('moo baby')
            client.ssl = True
            feed = client.GetDocList(auth_token=gdata.gauth.AeLoad(users.get_current_user().user_id())) #auth_token=TOKEN
            self.response.out.write(feed)
            self.response.out.write('moo boobob')
            self.response.headers['Content-Type'] = 'text/plain'
            for entry in feed.entry:
                self.response.out.writeln(entry.title.text)
        else:
            # Get unauthorized request token
            gdata.gauth.AeDelete(users.get_current_user().user_id())
            client = gdata.docs.client.DocsClient(source=SETTINGS['APP_NAME'])
            client.ssl = True # Force communication through HTTPS

            oauth_callback_url = ('http://%s/get_oauth_token' %
                                  self.request.host)

            request_token = client.GetOAuthToken(
                SETTINGS['SCOPES'], oauth_callback_url, SETTINGS['CONSUMER_KEY'],
                consumer_secret=SETTINGS['CONSUMER_SECRET'])
            gdata.gauth.AeSave(request_token, "tmp_"+users.get_current_user().user_id())
            # Authorize request token
            domain = None#'cornellsun.com'
            self.redirect(str(request_token.generate_authorization_url(google_apps_domain=domain)))

我已经在网络上四处寻找答案,但是没有找到。

3个回答

3

可能需要Aeoid(或其他OpenID库)来实现OAuth + OpenID组合。 - iamgopal

1
我刚刚发现浪费了几个小时,即使URL不正确,你也会得到401错误。
在我的例子中,我正在做的是:
.../buzz/v1/activities/@me/@self**?&**alt=json

不是

.../buzz/v1/activities/@me/@self**?**alt=json

0

我个人没有使用过OAuth,但我注意到几件事可能会(或可能不会)有所帮助:

  1. 401错误很可能是HTTP 401错误,这意味着URL有效,但需要验证。这显然是由OAuth尝试失败引起的,但重定向未登录用户到另一页也可能很重要。

  2. 当您分配您的feed变量时出现错误。 auth_token参数只是一个用户名吗?

3.您正在使用该行。

gdata.gauth.AeLoad(users.get_current_user().user_id())

经常。即使它可能与您的身份验证问题无关,您最好将此查询一次并将其存储在变量中。然后,当您再次需要它时,以这种方式访问它。它将提高应用程序的速度。

再次,我很抱歉我没有具体的OAuth经验。我只是试图扫描并找到一些可能引导您走上正确道路的东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接