|
13 | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
| 16 | +from __future__ import print_function |
16 | 17 |
|
17 |
| -import webbrowser |
18 | 18 | from requests_oauthlib import OAuth1Session
|
| 19 | +import webbrowser |
19 | 20 |
|
20 | 21 | REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
|
21 | 22 | ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
|
|
26 | 27 | def get_access_token(consumer_key, consumer_secret):
|
27 | 28 | oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret, callback_uri='oob')
|
28 | 29 |
|
29 |
| - print 'Requesting temp token from Twitter' |
| 30 | + print('\nRequesting temp token from Twitter...\n') |
30 | 31 |
|
31 | 32 | try:
|
32 | 33 | 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 | + |
36 | 37 | url = oauth_client.authorization_url(AUTHORIZATION_URL)
|
37 | 38 |
|
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)) |
46 | 44 |
|
47 | 45 | webbrowser.open(url)
|
48 |
| - pincode = raw_input('Pincode? ') |
| 46 | + pincode = input('\nEnter your pincode? ') |
49 | 47 |
|
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') |
53 | 49 |
|
54 | 50 | oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
|
55 | 51 | resource_owner_key=resp.get('oauth_token'),
|
56 | 52 | resource_owner_secret=resp.get('oauth_token_secret'),
|
57 |
| - verifier=pincode |
58 |
| - ) |
| 53 | + verifier=pincode) |
59 | 54 | try:
|
60 | 55 | 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) |
64 | 58 |
|
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'))) |
68 | 68 |
|
69 | 69 |
|
70 | 70 | 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: ') |
73 | 73 | get_access_token(consumer_key, consumer_secret)
|
74 | 74 |
|
75 | 75 |
|
|
0 commit comments