-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_utils.py
40 lines (27 loc) · 1.46 KB
/
twitter_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import oauth2
import constants
import urllib.parse as urlparse
consumer = oauth2.Consumer(constants.CONSUMER_KEY, constants.CONSUMER_SECRET)
def get_request_token():
client = oauth2.Client(consumer)
response, content = client.request(constants.REQUEST_TOKEN_URL, 'POST')
if response.status != 200:
print("An error occurred getting the request token from Twitter!")
return dict(urlparse.parse_qsl(content.decode('utf-8')))
def get_oauth_verifier(request_token):
# ask the user to authorize the app and give us the pin code
print("Go to the following site in your browser: ")
print(get_oauth_verifier_url(request_token))
return input('insert the pin: ')
def get_oauth_verifier_url(request_token):
return "{}?oauth_token={}".format(constants.AUTHORIZATION_URL, request_token['oauth_token'])
def get_access_token(request_token, oauth_verifier):
# Create a Token object which contains the request token and the verifier
token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
# Create a client with our consumer(our app) and the newly created(and verified) token
client = oauth2.Client(consumer, token)
# Ask Twitter for an access token, and Twitter knows it should give us because we
# verified the request token
response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
return dict(urlparse.parse_qsl(content.decode('utf-8')))