user2596604
user2596604

Reputation: 1

Getting a Python error (AttributeError: 'dict' object has no attribute 'read') trying to update code for Twitter Oauth2

I am trying to get some code to work to create a variable 'pyresponse' accessing the twitter stream. The code I have to work with is obsolete as it was not meant for oauth2. I have received advice on how to update it but it is still not working.

here is the code (I've removed my access and consumer keys here but those are in my working code):

import oauth2 as oauth
import urllib2 as urllib
import json

access_token_key = "..."
access_token_secret = "..."

consumer_key = "..."
consumer_secret = "..."

_debug = 0

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)

signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()

http_method = "GET"


http_handler  = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)

'''
Construct, sign, and open a twitter request
using the hard-coded credentials above.
'''
def twitterreq(url, method, parameters):
  req = oauth.Request.from_consumer_and_token(oauth_consumer,
                                             token=oauth_token,
                                             http_method=http_method,
                                             http_url=url, 
                                             parameters=parameters)

  req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)

  headers = req.to_header()

  if http_method == "POST":
    encoded_post_data = req.to_postdata()
  else:
    encoded_post_data = None
    url = req.to_url()

  opener = urllib.OpenerDirector()
  opener.add_handler(http_handler)
  opener.add_handler(https_handler)

  response = opener.open(url, encoded_post_data)

  return response

def fetchsamples():
  url = "https://api.twitter.com/1.1/search/tweets.json?q=microsoft"
  parameters = []
  response = twitterreq(url, "GET", parameters)
  return json.load(response)
#  for line in response:
#    print line.strip()

#if __name__ == '__main__':
#   fetchsamples()

pyresponse =  json.load(fetchsamples()) 
print pyresponse.keys()

And when I try to run it I get this error:

AttributeError: 'dict' object has no attribute 'read'

addendum: here is the full error:

Traceback (most recent call last):
  File "new.py", line 65, in <module>
    pyresponse =  json.load(fetchsamples()) 
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 274, in load
AttributeError: 'dict' object has no attribute 'read'

Upvotes: 0

Views: 22580

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122142

Your fetchsamples() method already returned a decoded value:

def fetchsamples():
  url = "https://api.twitter.com/1.1/search/tweets.json?q=microsoft"
  parameters = []
  response = twitterreq(url, "GET", parameters)
  return json.load(response)

But you then try to decode the result again:

pyresponse =  json.load(fetchsamples()) 

json.load() then complains that fetchsamples() returned a python dictionary. Just remove the json.load() call from that line:

pyresponse =  fetchsamples()
print pyresponse.keys()

Upvotes: 1

Related Questions