blob: 740baf249f3f8bcfa78cc9f5b838ee2b836a0caa [file] [log] [blame]
ade@google.comc7da92b2010-10-25 20:03:48 -04001# 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.com46aad172011-01-04 00:14:17 +000014from apiclient.discovery import build
15from apiclient.oauth import FlowThreeLegged
16
17import datetime
ade@google.comc7da92b2010-10-25 20:03:48 -040018import httplib2
19import logging
20import oauth_wrap
ade@google.com46aad172011-01-04 00:14:17 +000021import optparse
ade@google.comc7da92b2010-10-25 20:03:48 -040022import os
23import sys
24
ade@google.com46aad172011-01-04 00:14:17 +000025
ade@google.comc7da92b2010-10-25 20:03:48 -040026def load_properties_file(path):
ade@google.com60a53c02011-01-10 02:33:17 +000027 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.comc7da92b2010-10-25 20:03:48 -040036
ade@google.com46aad172011-01-04 00:14:17 +000037
38def save_properties(consumer_key, consumer_secret, token_key, token_secret, path):
ade@google.com60a53c02011-01-10 02:33:17 +000039 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.com46aad172011-01-04 00:14:17 +000051
ade@google.comc7da92b2010-10-25 20:03:48 -040052def fetch(url):
ade@google.com60a53c02011-01-10 02:33:17 +000053 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.com46aad172011-01-04 00:14:17 +000061
ade@google.com60a53c02011-01-10 02:33:17 +000062 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.comc7da92b2010-10-25 20:03:48 -040075
ade@google.com46aad172011-01-04 00:14:17 +000076
77def buzz_login():
ade@google.com60a53c02011-01-10 02:33:17 +000078 buzz_discovery = build("buzz", "v1").auth_discovery()
ade@google.com46aad172011-01-04 00:14:17 +000079
ade@google.com60a53c02011-01-10 02:33:17 +000080 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.com46aad172011-01-04 00:14:17 +000087
ade@google.com60a53c02011-01-10 02:33:17 +000088 authorize_url = flow.step1_get_authorize_url()
ade@google.com46aad172011-01-04 00:14:17 +000089
ade@google.com60a53c02011-01-10 02:33:17 +000090 print 'Go to the following link in your browser:'
91 print authorize_url
92 print
ade@google.com46aad172011-01-04 00:14:17 +000093
ade@google.com60a53c02011-01-10 02:33:17 +000094 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.com46aad172011-01-04 00:14:17 +000098
ade@google.com60a53c02011-01-10 02:33:17 +000099 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.com46aad172011-01-04 00:14:17 +0000104def generic_login():
ade@google.com60a53c02011-01-10 02:33:17 +0000105 #TODO(ade) Implement support for other services
106 print 'Support for services other than Buzz is not implemented yet. Sorry.'
107
ade@google.com46aad172011-01-04 00:14:17 +0000108
109def login(options):
ade@google.com60a53c02011-01-10 02:33:17 +0000110 if options.buzz:
111 buzz_login()
112 else:
113 generic_login()
ade@google.com46aad172011-01-04 00:14:17 +0000114
115
ade@google.comc7da92b2010-10-25 20:03:48 -0400116def get_command(args):
ade@google.com60a53c02011-01-10 02:33:17 +0000117 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.comc7da92b2010-10-25 20:03:48 -0400123
124def configure_logging(options):
ade@google.com60a53c02011-01-10 02:33:17 +0000125 if options.verbose:
126 logging.basicConfig(level=logging.DEBUG)
127
ade@google.comc7da92b2010-10-25 20:03:48 -0400128
129def main():
ade@google.com237f6052011-01-26 14:48:24 +0000130 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.com60a53c02011-01-10 02:33:17 +0000132 '''
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.comc7da92b2010-10-25 20:03:48 -0400140
ade@google.com60a53c02011-01-10 02:33:17 +0000141 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.comc7da92b2010-10-25 20:03:48 -0400158
159if __name__ == '__main__':
ade@google.com60a53c02011-01-10 02:33:17 +0000160 main()