Reputation: 1859
I want to get a Twitter access token via a web form hosted on Google app engine. I wrote a python code to do so. While it runs fairly well on the terminal on my local machine, it is giving an error when I deploy it on Google App engine sdk.
'NoneType' object has no attribute '__getitem__'
The code for the same is :
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
The setup_oauth
is called from the post method of the class where the user is redirected after clicking the button.
The traceback:
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__'
Now if I do dir(credentials)
I get this as output
['__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']
which clearly has __getitem__
. I don't get why it is showing the error still.
Upvotes: 0
Views: 346
Reputation: 251
Google App engine does not receive instructions like: verifier = raw_input('Please input the verifier: ') because is a web platform not a shell. It is necesary to have a way to enter information in a httprequest httpresponse way. "verifier" can be inputted to your appliation by using webapp, django, etc.
Upvotes: 0
Reputation: 12986
The TypeError: 'NoneType' object has no attribute '__getitem__'
error is because you are trying to call __getitem___
on the result of calling credentials.get('oauth_token')
which is None
not a list.
You should always check what is returned from calls like this, rather than assuming you got the expected result, or wrap it in a try block.
Upvotes: 1