|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 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. |
| 14 | + |
| 15 | +"""Demonstrates how to authenticate to Google Cloud Platform APIs using the |
| 16 | +Requests HTTP library.""" |
| 17 | + |
| 18 | +import argparse |
| 19 | + |
| 20 | + |
| 21 | +def implicit(): |
| 22 | + import google.auth |
| 23 | + from google.auth.transport import requests |
| 24 | + |
| 25 | + # Get the credentials and project ID from the environment. |
| 26 | + credentials, project = google.auth.default( |
| 27 | + scopes=['https://www.googleapis.com/auth/cloud-platform']) |
| 28 | + |
| 29 | + # Create a requests Session object with the credentials. |
| 30 | + session = requests.AuthorizedSession(credentials) |
| 31 | + |
| 32 | + # Make an authenticated API request |
| 33 | + response = session.get( |
| 34 | + 'https://www.googleapis.com/storage/v1/b'.format(project), |
| 35 | + params={'project': project}) |
| 36 | + response.raise_for_status() |
| 37 | + buckets = response.json() |
| 38 | + print(buckets) |
| 39 | + |
| 40 | + |
| 41 | +def explicit(project): |
| 42 | + from google.auth.transport import requests |
| 43 | + from google.oauth2 import service_account |
| 44 | + |
| 45 | + # Construct service account credentials using the service account key |
| 46 | + # file. |
| 47 | + credentials = service_account.Credentials.from_service_account_file( |
| 48 | + 'service_account.json') |
| 49 | + credentials = credentials.with_scopes( |
| 50 | + ['https://www.googleapis.com/auth/cloud-platform']) |
| 51 | + |
| 52 | + # Create a requests Session object with the credentials. |
| 53 | + session = requests.AuthorizedSession(credentials) |
| 54 | + |
| 55 | + # Make an authenticated API request |
| 56 | + response = session.get( |
| 57 | + 'https://www.googleapis.com/storage/v1/b'.format(project), |
| 58 | + params={'project': project}) |
| 59 | + response.raise_for_status() |
| 60 | + buckets = response.json() |
| 61 | + print(buckets) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + parser = argparse.ArgumentParser( |
| 66 | + description=__doc__, |
| 67 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 68 | + |
| 69 | + subparsers = parser.add_subparsers(dest='command') |
| 70 | + subparsers.add_parser('implicit', help=implicit.__doc__) |
| 71 | + explicit_parser = subparsers.add_parser('explicit', help=explicit.__doc__) |
| 72 | + explicit_parser.add_argument('project') |
| 73 | + |
| 74 | + args = parser.parse_args() |
| 75 | + |
| 76 | + if args.command == 'implicit': |
| 77 | + implicit() |
| 78 | + elif args.command == 'explicit': |
| 79 | + explicit(args.project) |
0 commit comments