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?
The rauth
library builds on top of the requests
library, returning a specialised requests.Session
object.
The object returned by session.get()
is a requests.Response
object; call the response.json()
method 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.