I needed to access the Twitter API, so I tried implementing it myself, but ... well ... I mean ... it was a hassle. So, when I searched for a library, I found a library called rauth, so I tried it.
Since it is an OAuth library (not Twitter library), APIs other than Twitter can be used as long as it is OAuth authentication. Also, it depends on Requests, and it feels good to be able to write the code of the HTTP request part neatly.
Below is a sample of muttering'rauth'on my timeline. "Hey, isn't it easy?"
rauth_sample
import rauth
# access_token/access_token_Premise of getting a secret
session = rauth.OAuth1Session(
"consumer_key",
"consumer_secret",
"access_token",
"access_token_secret")
#There is a way to set the baseURL to session, but it is omitted.
session.post(
'https://api.twitter.com/1.1/statuses/update.json',
data={'status': 'rauth'})
In the above sample, it is assumed that access_token / access_token_secret has been acquired in advance, but as far as the README is seen, it seems that it is not so difficult to acquire. Let's try this again soon.
By the way, she couldn't.
API baseURL can be omitted by setting OAuth1Service to OAuth1Session.
rauth_sample_set_service
twitter = rauth.OAuth1Service(
name='twitter',
consumer_key='consumer_key',
consumer_secret='consumer_secret',
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/')
session = rauth.OAuth1Session(
consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token='access_token',
access_token_secret='access_token_secret',
service=twitter)
#The baseURL set in Service can be omitted
res = session.get(
url='statuses/home_timeline.json',
params={'count': 10})
print(res.json())
Recommended Posts