2012-02-10 54 views
1

我目前正在嘗試開發GAE應用程序。使用Access Token一旦被接受?

我遵循本教程爲了使用OAuth訪問gdoc。 http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html

我不明白的是如何重用此令牌後。

如果我直接嘗試訪問第二個連接上的提要,由於缺乏授權(401),我反彈了。 我想我必須每次使用我的Gdocs對象中的access_token進行身份驗證,但找不到正確的方式來執行此操作。

任何想法?

我的代碼是一樣的,在教程中,加:

# Create an instance of the DocsService to make API calls 
gdocs = gdata.docs.client.DocsClient(source = SETTINGS['APP_NAME']) 

class MainPage(webapp.RequestHandler): 
@login_required 
def get(self): 
    my_user = users.get_current_user() 

    # I think I have to authenticate, but don't know how here 
    token_key = 'access_token_%s' % my_user.user_id() 
    access_token = gdata.gauth.ae_load(token_key) 
    gdocs.auth_token = gdocs.get_access_token(access_token) 

    feed = gdocs.GetResources() 
    for entry in feed.entry: 
     template = '<div>%s</div>' 
     self.response.out.write(template % entry.title.text) 

在缺省情況下啓動。

我知道我可以使用ae_load(token_key)訪問令牌密鑰,因爲我可以打印它。 我應該使用gdocs進行身份驗證,或更一般的客戶端,如下所示我: http://ikaisays.com/2011/05/26/setting-up-an-oauth-provider-on-google-app-engine/

我檢查,我的令牌在我的私人谷歌帳戶中。

感謝提前!

回答

1

我終於通過在google apis中搜索來回答我自己的問題。

這是我如何使用它:

access_token_key = 'access_token_%s' % user.user_id() 
    access_token = gdata.gauth.ae_load(access_token_key)  
    # authenticating here 
    #gdocs.auth_token = access_token 

另一個版本,將是這樣的:

#gdocs.auth_token = gdata.gauth.OAuthHmacToken(SETTINGS['CONSUMER_KEY'], 
    #            SETTINGS['CONSUMER_SECRET'], 
    #            access_token, 
    #            access_token_key, 
    #            gdata.gauth.ACCESS_TOKEN) 

因爲各國谷歌的文檔。

1

您應該存儲auth_token或數據以將其重建到數據存儲中。然後,在創建請求對象時,傳入auth_token。這在GDClient doc string中進行了解釋。

您可能還會發現有關fetching gdata feeds的文章很有用。

+0

感謝您的回答,但第二個鏈接完全對應我的代碼,正如我所說的。缺少的是下一步。 – jlengrand 2012-02-15 16:56:17

相關問題