-
Notifications
You must be signed in to change notification settings - Fork 3
/
twitter.py
57 lines (50 loc) · 2.24 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
from requests_oauthlib import OAuth1Session
consumer_key = 'xxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
resource_owner_key = ''
resource_owner_secret = ''
def twitter_get_oauth_request_token():
global resource_owner_key
global resource_owner_secret
request_token = OAuth1Session(client_key=consumer_key, client_secret=consumer_secret)
url = 'https://api.twitter.com/oauth/request_token'
data = request_token.get(url)
print(data.text)
data_token = str.split(data.text, '&')
ro_key = str.split(data_token[0], '=')
ro_secret = str.split(data_token[1], '=')
resource_owner_key = ro_key[1]
resource_owner_secret = ro_secret[1]
resource = [resource_owner_key, resource_owner_secret]
return resource
def twitter_get_oauth_token(verifier, ro_key, ro_secret):
oauth_token = OAuth1Session(client_key=consumer_key,
client_secret=consumer_secret,
resource_owner_key=ro_key,
resource_owner_secret=ro_secret)
url = 'https://api.twitter.com/oauth/access_token'
data = {"oauth_verifier": verifier}
print(ro_key)
print(ro_secret)
access_token_data = oauth_token.post(url, data=data)
print(access_token_data.text)
access_token_list = str.split(access_token_data.text, '&')
return access_token_list
def twitter_get_access_token(access_token_list):
access_token_key = str.split(access_token_list[0], '=')
access_token_secret = str.split(access_token_list[1], '=')
access_token_name = str.split(access_token_list[3], '=')
access_token_id = str.split(access_token_list[2], '=')
key = access_token_key[1]
secret = access_token_secret[1]
name = access_token_name[1]
id = access_token_id[1]
oauth_user = OAuth1Session(client_key=consumer_key,
client_secret=consumer_secret,
resource_owner_key=key,
resource_owner_secret=secret)
url_user = 'https://api.twitter.com/1.1/account/verify_credentials.json'
params = {"include_email": 'true'}
user_data = oauth_user.get(url_user, params=params)
print(user_data.json())
return user_data.json()