2010-05-14 155 views
5

我试图在Google App Engine中为我的学校报纸完成故事分配系统。它将跟踪作者的截止日期,允许作者选择故事,并对“周报”一目了然。我和我的合作伙伴正试图将其与Google Apps安装报告完全整合。哦,而且我们必须使用3脚Oauth,因为我们没有Google Apps Premier。Oauth + Aeoid + Python + Google App Engine + Google文档

在那个努力中,我偶然发现了Aeoid并能够按照说明进行联合登录工作。非常酷!

我遇到麻烦的地方是使用Oauth获取用户的Google文档列表。我在这里设置了一个测试页面:mustrun.cornellsun.com/test。它给了我错误 - 我在这封邮件的底部复制了它们。我不知道这是否与我的消费者秘密有关(我应该使用我从谷歌市场获得的密钥?还是应该使用我从管理域页面获得的密钥?)。现在我正在使用我从管理域页面获得的密钥

此外,更复杂的是,实际的appspot域为mustrun2sun [] .appspot [太新不能发布多个链接] .com,但是我在谷歌应用程序中设置它,以便只有来自我的域的用户才能登录,并且还可以将应用程序部署到我的域中。 (应用程序被部署为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数据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> 

而且,因为这是很难W/O任何源代码,以下是相关代码:

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

我一直在网上寻找答案&我一直未能找到一个。

回答

0

我个人不使用OAuth的工作,但几件事情,我注意到,可能(也可能不会)帮助:

  1. 的401错误可能是一个HTTP 401错误,这意味着网址是有效但需要的认证。这显然是由失败的OAuth尝试解释的,但将未登录到其他页面的用户重定向也可能很重要。

  2. 分配Feed变量时发生错误。 auth_token参数是否应该是一个用户名?

3.您正在使用该线。

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

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

再次,我很抱歉,我没有具体的OAuth经验。我只是试图扫描并发现一些可能会让你走上正确道路的事情。

1

我刚刚发现浪费了几个小时,如果URL不正确,你也会得到401。

在我的例子,我在做

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

而不是

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