我希望通过Google应用引擎上托管的网络表单获取Twitter访问令牌。我写了一个python代码来做到这一点。虽然它在我本地计算机上的终端上运行得相当好,但是当我在Google App引擎sdk上部署它时会出错。
'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
从类的post方法调用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 :(得分:1)
TypeError: 'NoneType' object has no attribute '__getitem__'
错误是因为您尝试拨打__getitem___
调用credentials.get('oauth_token')
None
而不是列表的结果。
你应该总是检查从这样的调用返回的内容,而不是假设你得到了预期的结果,或者将它包装在try块中。
答案 1 :(得分:0)
Google App引擎未收到如下说明: verifier = raw_input('请输入验证者:') 因为是一个web平台而不是shell。 有必要以httprequest httpresponse方式输入信息。 可以使用webapp,django等将“verifier”输入到您的应用程序中