2014-03-25 56 views
0

我想通過託管在Google應用引擎上的網絡表單獲取Twitter訪問令牌。我寫了一個python代碼來這樣做。雖然它在本地計算機上的終端上運行良好,但在將它部署到Google App引擎sdk時發生錯誤。在Google應用引擎上託管的twitter應用獲取訪問令牌

'NoneType' object has no attribute '__getitem__' 

對於相同的代碼是:

import sys 
import requests,sys 
sys.path.insert(0,'requests_oauthlib') 
from requests_oauthlib import OAuth1 
from urlparse import parse_qs 

REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token" 
AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token=" 
ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token" 

CONSUMER_KEY = "XXXXX" 
CONSUMER_SECRET = "XXXXX" 

OAUTH_TOKEN = "" 
OAUTH_TOKEN_SECRET = "" 

def setup_oauth(): 
    """Authorize your app via identifier.""" 
    # Request token 

    oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET) 
    r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth) 
    credentials = parse_qs(r.content) 

    resource_owner_key = credentials.get('oauth_token')[0] 
    resource_owner_secret = credentials.get('oauth_token_secret')[0] 

    # Authorize 
    authorize_url = AUTHORIZE_URL + resource_owner_key 
    print 'Please go here and authorize: ' + authorize_url 

    verifier = raw_input('Please input the verifier: ') 
    oauth = OAuth1(CONSUMER_KEY, 
        client_secret=CONSUMER_SECRET, 
        resource_owner_key=resource_owner_key, 
        resource_owner_secret=resource_owner_secret, 
        verifier=verifier) 

    # Finally, Obtain the Access Token 
    r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth) 
    credentials = parse_qs(r.content) 
    token = credentials.get('oauth_token')[0] 
    secret = credentials.get('oauth_token_secret')[0] 

    return token, secret 

def get_oauth(): 
    oauth = OAuth1(CONSUMER_KEY, 
       client_secret=CONSUMER_SECRET, 
       resource_owner_key=OAUTH_TOKEN, 
       resource_owner_secret=OAUTH_TOKEN_SECRET) 
    return oauth 

setup_oauth從類的交方法,其中向用戶點擊按鈕之後重定向調用。

回溯:

Traceback (most recent call last): 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/apps/s~twitterlookback/2.374658065002598509/testing.py", line 86, in post 
    token, secret = setup_oauth() 
    File "/base/data/home/apps/s~twitterlookback/2.374658065002598509/testing.py", line 43, in setup_oauth 
    resource_owner_key = credentials.get('oauth_token')[0] 
TypeError: 'NoneType' object has no attribute '__getitem__' 

現在,如果我這樣做dir(credentials)我得到這個作爲輸出

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] 

這顯然有__getitem__。我不明白爲什麼它仍然顯示錯誤。

+0

@TimHoffman被輸入到您的appliation但它工作的本地計算機上細 – jairaj

+0

是的,它可以很好地工作,因爲你調用返回的東西,可以讓[]的訪問。但是你並沒有檢查通話中返回的內容。你會在Twitter上失去一些你認證的東西。 –

+0

爲什麼它不適用於Google App引擎?即使憑據正在返回具有__getitem__的內容。 – jairaj

回答

1

TypeError: 'NoneType' object has no attribute '__getitem__'錯誤是因爲您試圖撥打電話__getitem___credentials.get('oauth_token')這是None不是一個列表的結果。

您應該始終檢查這樣的調用返回的內容,而不是假設您獲得了預期的結果,或者將其包裝在try塊中。

0

谷歌應用程序引擎不會收到像這樣的指令: verifier = raw_input('請輸入驗證者:') 因爲是一個web平臺而不是shell。 有必要以httprequest httpresponse方式輸入信息。 「驗證」可以通過使用Web應用程序,Django的,等等

相關問題