【问题标题】:Get access token for twitter application hosted on google app engine获取托管在谷歌应用引擎上的 twitter 应用程序的访问令牌
【发布时间】: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


【解决方案1】:

TypeError: 'NoneType' object has no attribute '__getitem__' 错误是因为您尝试在调用 credentials.get('oauth_token') 的结果上调用 __getitem___,这是 None 而不是列表。

您应该始终检查从这样的调用返回的内容,而不是假设您得到了预期的结果,或者将其包装在 try 块中。

【讨论】:

    【解决方案2】:

    Google App 引擎未收到以下指令: verifier = raw_input('请输入验证者:') 因为是网络平台而不是外壳。 必须有一种以 httprequest httpresponse 方式输入信息的方法。 可以使用 webapp、django 等将“验证器”输入到您的应用程序中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-30
      • 2016-07-14
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多