forked from bear/python-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_access_token.py
executable file
·78 lines (62 loc) · 2.61 KB
/
get_access_token.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
#!/usr/bin/env python
#
# Copyright 2007-2013 The Python-Twitter Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import webbrowser
from requests_oauthlib import OAuth1Session
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
def get_access_token(consumer_key, consumer_secret):
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret)
print 'Requesting temp token from Twitter'
try:
resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)
except ValueError, e:
print 'Invalid respond from Twitter requesting temp token: %s' % e
return
url = oauth_client.authorization_url(AUTHORIZATION_URL)
print ''
print 'I will try to start a browser to visit the following Twitter page'
print 'if a browser will not start, copy the URL to your browser'
print 'and retrieve the pincode to be used'
print 'in the next step to obtaining an Authentication Token:'
print ''
print url
print ''
webbrowser.open(url)
pincode = raw_input('Pincode? ')
print ''
print 'Generating and signing request for an access token'
print ''
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
resource_owner_key=resp.get('oauth_token'),
resource_owner_secret=resp.get('oauth_token_secret'),
verifier=pincode
)
try:
resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)
except ValueError, e:
print 'Invalid respond from Twitter requesting access token: %s' % e
return
print 'Your Twitter Access Token key: %s' % resp.get('oauth_token')
print ' Access Token secret: %s' % resp.get('oauth_token_secret')
print ''
def main():
consumer_key = raw_input('Enter your consumer key: ')
consumer_secret = raw_input("Enter your consumer secret: ")
get_access_token(consumer_key, consumer_secret)
if __name__ == "__main__":
main()