-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.py
executable file
·100 lines (63 loc) · 2.83 KB
/
oauth.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
import dbm
from urlparse import parse_qs
from user_credentials_dbm import dbm_create, dbm_fetch
from packages.clint import resources
from packages import requests
from packages.requests_oauthlib import OAuth1
# these are the client key and secret needed
# for oauth authentication with the twitter api
client_key = "moNHqPmAoEvS21mpajk6lNXl3"
client_secret = "WXQHh9tykUivPFiVGQlwXhjOqR1S3y6W6z8LojSRCuw3vwYUeT"
# these are the user credentials for oauth authentication
# with the twitter api
user_token = ""
user_secret = ""
def get_token(client_key, client_secret, user_token, user_secret):
oauth = OAuth1(client_key, client_secret)
url = 'https://api.twitter.com/oauth/request_token'
token_requests = requests.post(url, auth=oauth)
print token_requests
credentials = parse_qs(token_requests.content)
user_token = credentials.get('oauth_token')[0]
user_secret = credentials.get('oauth_token_secret')[0]
return user_token, user_secret
def authorize(client_key, client_secret, user_token, user_secret):
url = 'https://api.twitter.com/oauth/authorize?oauth_token=' + user_token
print ''
print 'Copy Paste in your browser this link %s' % url
print (
'Click the authorize button to give TweeтCoммander access to your'
' twitter account information and put the pin code in the prompt'
' below.'
)
print ''
print '+----------------------------------------------+'
verifier = raw_input('Put your PIN code here: ')
return verifier
def get_access(client_key, client_secret, user_token, user_secret, verifier):
url = 'https://api.twitter.com/oauth/access_token'
oauth = OAuth1(client_key,
client_secret,
user_token,
user_secret,
verifier=verifier)
access_request = requests.post(url=url, auth=oauth)
credentials = parse_qs(access_request.content)
user_token = credentials.get('oauth_token')[0]
user_secret = credentials.get('oauth_token_secret')[0]
dbm_create(user_token, user_secret)
def connection(client_key=client_key, client_secret=client_secret):
user_token, user_secret = dbm_fetch()
oauth = OAuth1(client_key, client_secret, user_token, user_secret)
return oauth
def first_time_oauth(client_key=client_key, client_secret=client_secret,
user_token=user_token, user_secret=user_secret):
print (
'This is your first time with TweeтCoммander.'
' Please follow the instructions below.'
)
user_token, user_secret = get_token(client_key, client_secret,
user_token, user_secret)
verifier = authorize(client_key, client_secret, user_token, user_secret)
get_access(client_key, client_secret, user_token, user_secret, verifier)