ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 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. |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 14 | from apiclient.discovery import build |
| 15 | from apiclient.oauth import FlowThreeLegged |
| 16 | |
| 17 | import datetime |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 18 | import httplib2 |
| 19 | import logging |
| 20 | import oauth_wrap |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 21 | import optparse |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 22 | import os |
| 23 | import sys |
| 24 | |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 25 | |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 26 | def load_properties_file(path): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 27 | properties = {} |
| 28 | for line in open(path): |
| 29 | line = line.strip() |
| 30 | if line.startswith('#'): |
| 31 | continue |
| 32 | |
| 33 | key,value = line.split('=') |
| 34 | properties[key.strip()] = value.strip() |
| 35 | return properties |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 36 | |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 37 | |
| 38 | def save_properties(consumer_key, consumer_secret, token_key, token_secret, path): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 39 | file = open(path, 'w') |
| 40 | |
| 41 | # File format and order is based on oacurl.java's defaults |
| 42 | now = datetime.datetime.today() |
| 43 | now_string = now.strftime('%a %b %d %H:%m:%S %Z %Y') |
| 44 | file.write('#%s\n' % now_string) |
| 45 | file.write('consumerSecret=%s\n' % consumer_secret) |
| 46 | file.write('accessToken=%s\n' % token_key) |
| 47 | file.write('consumerKey=%s\n' % consumer_key) |
| 48 | file.write('accessTokenSecret=%s\n' % token_secret) |
| 49 | file.close() |
| 50 | |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 51 | |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 52 | def fetch(url): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 53 | logging.debug('Now fetching: %s' % url) |
| 54 | |
| 55 | path = os.path.expanduser('~/.oacurl.properties') |
| 56 | if not os.path.exists(path): |
| 57 | logging.debug('User is not logged in.') |
| 58 | |
| 59 | print 'You are not logged in' |
| 60 | sys.exit(1) |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 61 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 62 | properties = load_properties_file(path) |
| 63 | oauth_parameters = { |
| 64 | 'consumer_key': properties['consumerKey'], |
| 65 | 'consumer_secret' : properties['consumerSecret'], |
| 66 | 'oauth_token' : properties['accessToken'], |
| 67 | 'oauth_token_secret':properties['accessTokenSecret']} |
| 68 | |
| 69 | http = oauth_wrap.get_authorised_http(oauth_parameters) |
| 70 | response, content = http.request(url) |
| 71 | logging.debug(response) |
| 72 | logging.debug(content) |
| 73 | |
| 74 | return response,content |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 75 | |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 76 | |
| 77 | def buzz_login(): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 78 | buzz_discovery = build("buzz", "v1").auth_discovery() |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 79 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 80 | flow = FlowThreeLegged(buzz_discovery, |
| 81 | consumer_key='anonymous', |
| 82 | consumer_secret='anonymous', |
| 83 | user_agent='google-api-client-python-buzz-cmdline/1.0', |
| 84 | domain='anonymous', |
| 85 | scope='https://www.googleapis.com/auth/buzz', |
| 86 | xoauth_displayname='oacurl.py') |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 87 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 88 | authorize_url = flow.step1_get_authorize_url() |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 89 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 90 | print 'Go to the following link in your browser:' |
| 91 | print authorize_url |
| 92 | print |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 93 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 94 | accepted = 'n' |
| 95 | while accepted.lower() == 'n': |
| 96 | accepted = raw_input('Have you authorized me? (y/n) ') |
| 97 | verification = raw_input('What is the verification code? ').strip() |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 98 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 99 | credentials = flow.step2_exchange(verification) |
| 100 | path = os.path.expanduser('~/.oacurl.properties') |
| 101 | save_properties('anonymous', 'anonymous', credentials.token.key, credentials.token.secret,path) |
| 102 | |
| 103 | |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 104 | def generic_login(): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 105 | #TODO(ade) Implement support for other services |
| 106 | print 'Support for services other than Buzz is not implemented yet. Sorry.' |
| 107 | |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 108 | |
| 109 | def login(options): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 110 | if options.buzz: |
| 111 | buzz_login() |
| 112 | else: |
| 113 | generic_login() |
ade@google.com | 46aad17 | 2011-01-04 00:14:17 +0000 | [diff] [blame] | 114 | |
| 115 | |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 116 | def get_command(args): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 117 | if args and args[0] == 'login': |
| 118 | return 'login' |
| 119 | if args and args[0] == 'fetch': |
| 120 | return 'fetch' |
| 121 | return None |
| 122 | |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 123 | |
| 124 | def configure_logging(options): |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 125 | if options.verbose: |
| 126 | logging.basicConfig(level=logging.DEBUG) |
| 127 | |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 128 | |
| 129 | def main(): |
ade@google.com | 237f605 | 2011-01-26 14:48:24 +0000 | [diff] [blame] | 130 | usage = '''Usage: python %prog [options] fetch <url> |
| 131 | Example: python %prog -v fetch "https://www.googleapis.com/buzz/v1/people/@me/@self?alt=json&pp=1" |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 132 | ''' |
| 133 | parser = optparse.OptionParser(usage=usage) |
| 134 | parser.set_defaults(verbose=False) |
| 135 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose') |
| 136 | parser.add_option('-q', '--quiet', action='store_false', dest='verbose') |
| 137 | parser.add_option('--buzz', action='store_true', dest='buzz') |
| 138 | |
| 139 | (options, args) = parser.parse_args() |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 140 | |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 141 | configure_logging(options) |
| 142 | logging.debug('Options: %s and Args: %s' % (str(options), str(args))) |
| 143 | |
| 144 | command = get_command(args) |
| 145 | |
| 146 | if not command: |
| 147 | parser.error('Invalid arguments') |
| 148 | return |
| 149 | |
| 150 | if command == 'fetch': |
| 151 | response, content = fetch(args[1]) |
| 152 | print response |
| 153 | print content |
| 154 | return |
| 155 | |
| 156 | if command == 'login': |
| 157 | login(options) |
ade@google.com | c7da92b | 2010-10-25 20:03:48 -0400 | [diff] [blame] | 158 | |
| 159 | if __name__ == '__main__': |
ade@google.com | 60a53c0 | 2011-01-10 02:33:17 +0000 | [diff] [blame] | 160 | main() |