Joe Gregorio | 4c46b7b | 2012-03-20 14:29:56 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame^] | 4 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 4c46b7b | 2012-03-20 14:29:56 -0400 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """Simple command-line sample that demonstrates service accounts. |
| 19 | |
| 20 | Lists all the Google Task Lists associated with the given service account. |
| 21 | Service accounts are created in the Google API Console. See the documentation |
| 22 | for more information: |
| 23 | |
| 24 | https://developers.google.com/console/help/#WhatIsKey |
| 25 | |
| 26 | Usage: |
| 27 | $ python tasks.py |
| 28 | """ |
| 29 | |
| 30 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 31 | |
| 32 | import httplib2 |
| 33 | import pprint |
| 34 | import sys |
| 35 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 36 | from googleapiclient.discovery import build |
Joe Gregorio | 4c46b7b | 2012-03-20 14:29:56 -0400 | [diff] [blame] | 37 | from oauth2client.client import SignedJwtAssertionCredentials |
| 38 | |
| 39 | def main(argv): |
| 40 | # Load the key in PKCS 12 format that you downloaded from the Google API |
| 41 | # Console when you created your Service account. |
| 42 | f = file('key.p12', 'rb') |
| 43 | key = f.read() |
| 44 | f.close() |
| 45 | |
| 46 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 47 | # with the Credentials. Note that the first parameter, service_account_name, |
| 48 | # is the Email address created for the Service account. It must be the email |
| 49 | # address associated with the key that was created. |
| 50 | credentials = SignedJwtAssertionCredentials( |
| 51 | '141491975384@developer.gserviceaccount.com', |
| 52 | key, |
| 53 | scope='https://www.googleapis.com/auth/tasks') |
| 54 | http = httplib2.Http() |
| 55 | http = credentials.authorize(http) |
| 56 | |
| 57 | service = build("tasks", "v1", http=http) |
| 58 | |
| 59 | # List all the tasklists for the account. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 60 | lists = service.tasklists().list().execute(http=http) |
Joe Gregorio | 4c46b7b | 2012-03-20 14:29:56 -0400 | [diff] [blame] | 61 | pprint.pprint(lists) |
| 62 | |
| 63 | |
| 64 | if __name__ == '__main__': |
| 65 | main(sys.argv) |