2010-05-14 151 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