Skip to content

Commit 0028528

Browse files
committed
updates get_access_token & examples for python 3
1 parent 311c6bf commit 0028528

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

examples/view_friends.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
# Copyright 2016 The Python-Twitter Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import twitter
216

17+
CONSUMER_KEY = 'WWWWWWWW'
18+
CONSUMER_SECRET = 'XXXXXXXX'
19+
ACCESS_TOKEN = 'YYYYYYYY'
20+
ACCESS_TOKEN_SECRET = 'ZZZZZZZZ'
21+
22+
23+
# Create an Api instance.
324
api = twitter.Api(consumer_key='consumer_key',
425
consumer_secret='consumer_secret',
526
access_token_key='access_token',
627
access_token_secret='access_token_secret')
28+
729
users = api.GetFriends()
8-
print [u.name for u in users]
30+
31+
print([u.screen_name for u in users])

get_access_token.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
from __future__ import print_function
1617

17-
import webbrowser
1818
from requests_oauthlib import OAuth1Session
19+
import webbrowser
1920

2021
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
2122
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
@@ -26,50 +27,49 @@
2627
def get_access_token(consumer_key, consumer_secret):
2728
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret, callback_uri='oob')
2829

29-
print 'Requesting temp token from Twitter'
30+
print('\nRequesting temp token from Twitter...\n')
3031

3132
try:
3233
resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)
33-
except ValueError, e:
34-
print 'Invalid respond from Twitter requesting temp token: %s' % e
35-
return
34+
except ValueError as e:
35+
raise 'Invalid response from Twitter requesting temp token: {0}'.format(e)
36+
3637
url = oauth_client.authorization_url(AUTHORIZATION_URL)
3738

38-
print ''
39-
print 'I will try to start a browser to visit the following Twitter page'
40-
print 'if a browser will not start, copy the URL to your browser'
41-
print 'and retrieve the pincode to be used'
42-
print 'in the next step to obtaining an Authentication Token:'
43-
print ''
44-
print url
45-
print ''
39+
print('I will try to start a browser to visit the following Twitter page '
40+
'if a browser will not start, copy the URL to your browser '
41+
'and retrieve the pincode to be used '
42+
'in the next step to obtaining an Authentication Token: \n'
43+
'\n\t{0}'.format(url))
4644

4745
webbrowser.open(url)
48-
pincode = raw_input('Pincode? ')
46+
pincode = input('\nEnter your pincode? ')
4947

50-
print ''
51-
print 'Generating and signing request for an access token'
52-
print ''
48+
print('\nGenerating and signing request for an access token...\n')
5349

5450
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
5551
resource_owner_key=resp.get('oauth_token'),
5652
resource_owner_secret=resp.get('oauth_token_secret'),
57-
verifier=pincode
58-
)
53+
verifier=pincode)
5954
try:
6055
resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)
61-
except ValueError, e:
62-
print 'Invalid respond from Twitter requesting access token: %s' % e
63-
return
56+
except ValueError as e:
57+
raise 'Invalid response from Twitter requesting temp token: {0}'.format(e)
6458

65-
print 'Your Twitter Access Token key: %s' % resp.get('oauth_token')
66-
print ' Access Token secret: %s' % resp.get('oauth_token_secret')
67-
print ''
59+
print('''Your tokens/keys are as follows:
60+
consumer_key = {ck}
61+
consumer_secret = {cs}
62+
access_token_key = {atk}
63+
access_token_secret = {ats}'''.format(
64+
ck=consumer_key,
65+
cs=consumer_secret,
66+
atk=resp.get('oauth_token'),
67+
ats=resp.get('oauth_token_secret')))
6868

6969

7070
def main():
71-
consumer_key = raw_input('Enter your consumer key: ')
72-
consumer_secret = raw_input("Enter your consumer secret: ")
71+
consumer_key = input('Enter your consumer key: ')
72+
consumer_secret = input('Enter your consumer secret: ')
7373
get_access_token(consumer_key, consumer_secret)
7474

7575

0 commit comments

Comments
 (0)