-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.py
67 lines (56 loc) · 1.88 KB
/
twitter.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os, requests, sys
import requests_oauthlib
try:
int = long
except NameError:
pass
try:
import urllib.parse
quote = urllib.parse.quote
except ImportError:
import urllib
quote = urllib.quote
TWITTER_KEY = os.environ["TWITTER_KEY"]
TWITTER_SECRET = os.environ["TWITTER_SECRET"]
def make_oauth(resource_owner_key, resource_owner_secret):
return requests_oauthlib.OAuth1Session(
TWITTER_KEY,
client_secret=TWITTER_SECRET,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
)
def request_token(callback_uri):
''': (...) -> (AuthorizationUrl, ResourceOwnerKey, ResourceOwnerSecret)'''
oauth = requests_oauthlib.OAuth1Session(
TWITTER_KEY,
client_secret=TWITTER_SECRET,
callback_uri=callback_uri,
)
url = "https://api.twitter.com/oauth/request_token"
response = oauth.fetch_request_token(url)
resource_owner_key = response["oauth_token"]
resource_owner_secret = response["oauth_token_secret"]
url = "https://api.twitter.com/oauth/authorize"
authorization_url = oauth.authorization_url(url)
return (
authorization_url,
resource_owner_key,
resource_owner_secret,
)
def access_token(verifier, resource_owner_key, resource_owner_secret):
''': (...) -> (ResourceOwnerKey, ResourceOwnerSecret)'''
oauth = requests_oauthlib.OAuth1Session(
TWITTER_KEY,
client_secret=TWITTER_SECRET,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier,
)
url = "https://api.twitter.com/oauth/access_token"
response = oauth.fetch_access_token(url)
resource_owner_key = response["oauth_token"]
resource_owner_secret = response["oauth_token_secret"]
return (
resource_owner_key,
resource_owner_secret,
)