Skip to content
Browse files

updates get_access_token & examples for python 3

1 parent 311c6bf commit 00285283345b0d26f29fead8da80c79e0564837c @jeremylow jeremylow committed Mar 16, 2016
Showing with 51 additions and 28 deletions.
  1. +24 −1 examples/view_friends.py
  2. +27 −27 get_access_token.py
View
25 examples/view_friends.py
@@ -1,8 +1,31 @@
+# Copyright 2016 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 twitter
+CONSUMER_KEY = 'WWWWWWWW'
+CONSUMER_SECRET = 'XXXXXXXX'
+ACCESS_TOKEN = 'YYYYYYYY'
+ACCESS_TOKEN_SECRET = 'ZZZZZZZZ'
+
+
+# Create an Api instance.
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
+
users = api.GetFriends()
-print [u.name for u in users]
+
+print([u.screen_name for u in users])
View
54 get_access_token.py
@@ -13,9 +13,10 @@
# 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.
+from __future__ import print_function
-import webbrowser
from requests_oauthlib import OAuth1Session
+import webbrowser
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
@@ -26,50 +27,49 @@
def get_access_token(consumer_key, consumer_secret):
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret, callback_uri='oob')
- print 'Requesting temp token from Twitter'
+ print('\nRequesting temp token from Twitter...\n')
try:
resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)
- except ValueError, e:
- print 'Invalid respond from Twitter requesting temp token: %s' % e
- return
+ except ValueError as e:
+ raise 'Invalid response from Twitter requesting temp token: {0}'.format(e)
+
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 ''
+ print('I will try to start a browser to visit the following Twitter page '
+ 'if a browser will not start, copy the URL to your browser '
+ 'and retrieve the pincode to be used '
+ 'in the next step to obtaining an Authentication Token: \n'
+ '\n\t{0}'.format(url))
webbrowser.open(url)
- pincode = raw_input('Pincode? ')
+ pincode = input('\nEnter your pincode? ')
- print ''
- print 'Generating and signing request for an access token'
- print ''
+ print('\nGenerating and signing request for an access token...\n')
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
- )
+ 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
+ except ValueError as e:
+ raise 'Invalid response from Twitter requesting temp token: {0}'.format(e)
- print 'Your Twitter Access Token key: %s' % resp.get('oauth_token')
- print ' Access Token secret: %s' % resp.get('oauth_token_secret')
- print ''
+ print('''Your tokens/keys are as follows:
+ consumer_key = {ck}
+ consumer_secret = {cs}
+ access_token_key = {atk}
+ access_token_secret = {ats}'''.format(
+ ck=consumer_key,
+ cs=consumer_secret,
+ atk=resp.get('oauth_token'),
+ ats=resp.get('oauth_token_secret')))
def main():
- consumer_key = raw_input('Enter your consumer key: ')
- consumer_secret = raw_input("Enter your consumer secret: ")
+ consumer_key = input('Enter your consumer key: ')
+ consumer_secret = input('Enter your consumer secret: ')
get_access_token(consumer_key, consumer_secret)

0 comments on commit 0028528

Please sign in to comment.
Something went wrong with that request. Please try again.