How do I open the JSON response to my Twitter search query?
Question: I have this rauth-powered command-line Python script so far:

    
    
    import json
    from rauth import OAuth1Service
    
    twitter = OAuth1Service(
                name='twitter',
                consumer_key='[REDACTED]',
                consumer_secret='[REDACTED]',
                request_token_url='https://api.twitter.com/oauth/request_token',
                access_token_url='https://api.twitter.com/oauth/access_token',
                authorize_url='https://api.twitter.com/oauth/authorize',
                base_url='https://api.twitter.com/1.1/')
    
    request_token, request_token_secret = twitter.get_request_token()
    authorize_url = twitter.get_authorize_url(request_token)
    
    print 'Copy-paste this URL into your browser: ' + authorize_url
    twitter_pin = raw_input('Enter PIN: ')
    
    session = twitter.get_auth_session(request_token,
                                       request_token_secret,
                                       method='POST',
                                       data={'oauth_verifier': twitter_pin})
    
    search_results = session.get('search/tweets.json', params={'q':'example'})
    
    print json.dumps(search_results)
    

rauth does all the OAuth handling and I get an object in return. I try to
print it and it says Response [200] (in pointy brackets), indicating a
success. I try to use Python's json.dump to print out the contents and I get:

    
    
    TypeError: <Response [200]> is not JSON serializable
    

I'm probably overlooking something very small. What's wrong with this?

[Relevant Twitter
documentation](https://dev.twitter.com/docs/api/1.1/get/search/tweets)
[Relevant rauth
documentation](https://rauth.readthedocs.org/en/latest/api/#oauth-1-0-services)


Answer: The `rauth` library builds on top of the [`requests` library](http://python-
requests.org/), returning a specialised [`requests.Session`
object](http://docs.python-requests.org/en/latest/user/advanced/#session-
objects).

The object returned by [`session.get()`](http://docs.python-
requests.org/en/latest/api/#requests.Session.get) is a [`requests.Response`
object](http://docs.python-requests.org/en/latest/api/#requests.Response);
call the [`response.json()` method](http://docs.python-
requests.org/en/latest/api/#requests.Response.json) to get the JSON response
data as a Python structure:

    
    
    print search_results.json()
    

No need to dump that back into a JSON string, work directly with the Python
data structure.

