【发布时间】:2014-05-03 02:56:32
【问题描述】:
我想通过托管在 Google 应用引擎上的网络表单获取 Twitter 访问令牌。为此,我编写了一个 python 代码。虽然它在我本地机器上的终端上运行得相当好,但当我将它部署到 Google App engine 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
setup_oauth 是从用户点击按钮后被重定向的类的 post 方法调用的。
回溯:
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__。我不明白为什么它仍然显示错误。
【问题讨论】:
-
@TimHoffman 但在本地机器上运行良好
-
是的,它可能会很好地工作,因为您调用会返回允许 [] 访问的内容。但是您并没有检查电话返回的内容。在使用 twitter 进行身份验证时,你会遗漏一些东西。
-
为什么它不能在 Google App 引擎上运行?即使凭据返回具有 getitem 的内容。
-
看看我在下面和代码中写了什么,它是
credentials.get('oauth_token')的结果,你试图在credentials上调用__getitem__而不是credentials -
@TimHoffman 我应该做哪些更改才能使代码正常工作?
标签: python google-app-engine twitter oauth twitter-oauth