Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 1 | # Using OAuth 2.0 for Server to Server Applications |
| 2 | |
| 3 | The Google APIs Client Library for Python supports using OAuth 2.0 for server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.) |
| 4 | |
| 5 | Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses [Google Cloud Datastore](https://cloud.google.com/datastore/) for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API. |
| 6 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 7 | If you have a G Suite domain—if you use [G Suite](https://gsuite.google.com/), for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the [Google Calendar API](https://developers.google.com/calendar/) to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account. |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 8 | |
| 9 | > **Note:** When you use [G Suite Marketplace](https://www.google.com/enterprise/marketplace/) to install an application for your domain, the required permissions are automatically granted to the application. You do not need to manually authorize the service accounts that the application uses. |
| 10 | |
Liron Newman | fb6fcc8 | 2020-04-21 18:53:01 +0100 | [diff] [blame] | 11 | > **Note:** Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite Admin console to restrict the ability of G Suite end users to share documents outside of the domain would not apply to service accounts. Similarly, that policy would prevent users from sharing documents with service accounts, because service acounts are always outside of the domain. If you're using G Suite domain-wide delegation, this isn't relevant to you - you are accessing APIs while acting as a domain user, not as the service account itself. |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 12 | |
| 13 | This document describes how an application can complete the server-to-server OAuth 2.0 flow by using the Google APIs Client Library for Python. |
| 14 | |
| 15 | ## Overview |
| 16 | |
Liron Newman | fb6fcc8 | 2020-04-21 18:53:01 +0100 | [diff] [blame] | 17 | To support server-to-server interactions, first create a service account for your project in the API Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account. |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 18 | |
| 19 | Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server. |
| 20 | |
| 21 | Finally, your application can use the access token to call Google APIs. |
| 22 | |
| 23 | ## Creating a service account |
| 24 | |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 25 | https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 26 | |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 27 | |
| 28 | ## Delegating domain-wide authority to the service account |
| 29 | |
Liron Newman | fb6fcc8 | 2020-04-21 18:53:01 +0100 | [diff] [blame] | 30 | If your application runs in a G Suite domain and accesses user data, the service account that you created needs to be granted access to the user data that you want to access. |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 31 | |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 32 | https://developers.google.com/admin-sdk/directory/v1/guides/delegation |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 33 | |
| 34 | ## Preparing to make an authorized API call |
| 35 | |
| 36 | After you obtain the client email address and private key from the API Console, complete the following steps: |
| 37 | |
| 38 | 1. Install the required libraries: |
| 39 | |
| 40 | ```sh |
| 41 | pip install google-auth google-auth-httplib2 google-api-python-client |
| 42 | ``` |
| 43 | |
| 44 | 1. Create a `Credentials` object from the service account's credentials and the scopes your application needs access to. For example: |
| 45 | |
| 46 | ### Examples |
| 47 | |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 48 | #### Application Default Credentials |
| 49 | |
| 50 | Application Default Credentials abstracts authentication across the different Google Cloud Platform hosting environments. When running on any Google Cloud hosting environment or when running locally with the Google Cloud SDK installed, `google.auth.default()` can automatically determine the credentials from the environment. See https://google.aip.dev/auth/4110 and https://googleapis.dev/python/google-auth/latest/user-guide.html#application-default-credentials for details. |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 51 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 52 | ```python |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 53 | import google.auth |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 54 | |
| 55 | SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin'] |
| 56 | |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 57 | credentials, project = google.auth.default(scopes=SCOPES) |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 58 | ``` |
| 59 | |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 60 | #### Other Platforms |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 61 | Obtain a service account key file by following this guide: |
| 62 | https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 63 | ```python |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 64 | from google.oauth2 import service_account |
| 65 | |
| 66 | SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin'] |
| 67 | SERVICE_ACCOUNT_FILE = '/path/to/service.json' |
| 68 | |
| 69 | credentials = service_account.Credentials.from_service_account_file( |
| 70 | SERVICE_ACCOUNT_FILE, scopes=SCOPES) |
| 71 | ``` |
| 72 | |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 73 | Use the `credentials` object to call Google APIs in your application. |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 74 | |
Liron Newman | fb6fcc8 | 2020-04-21 18:53:01 +0100 | [diff] [blame] | 75 | #### Using Domain-wide Delegation |
| 76 | |
| 77 | ```python |
| 78 | from google.oauth2 import service_account |
| 79 | |
| 80 | SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin'] |
| 81 | SERVICE_ACCOUNT_FILE = '/path/to/service.json' |
| 82 | |
| 83 | credentials = service_account.Credentials.from_service_account_file( |
| 84 | SERVICE_ACCOUNT_FILE, scopes=SCOPES, subject='user@domain.com') |
| 85 | ``` |
| 86 | |
Bu Sun Kim | b2ea122 | 2020-12-04 12:02:01 -0700 | [diff] [blame^] | 87 | Use the `credentials` object to call Google APIs in your application. The API requests would be authorized as `user@domain.com`, if you've authorized the service account accordingly in the G Suite Admin console. |
Liron Newman | fb6fcc8 | 2020-04-21 18:53:01 +0100 | [diff] [blame] | 88 | |
| 89 | |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 90 | ## Calling Google APIs |
| 91 | |
| 92 | To call a Google API using the `Credentials` object, complete the following steps: |
| 93 | |
| 94 | 1. Build a service object for the API that you want to call. You build a a service object by calling the build function with the name and version of the API and the authorized Http object. For example, to call version 1beta3 of the [Cloud SQL Administration API](https://cloud.google.com/sql/docs/admin-api/): |
| 95 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 96 | ```python |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 97 | import googleapiclient.discovery |
| 98 | |
| 99 | sqladmin = googleapiclient.discovery.build('sqladmin', 'v1beta3', credentials=credentials) |
| 100 | ``` |
| 101 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 102 | 1. Make requests to the API service using the interface provided by the service object. For example, to list the instances of Cloud SQL databases in the example-123 project: |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 103 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 104 | ```python |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 105 | response = sqladmin.instances().list(project='example-123').execute() |
| 106 | ``` |
| 107 | |
| 108 | ## Complete example |
| 109 | |
| 110 | The following example prints a JSON-formatted list of Cloud SQL instances in a project. |
| 111 | |
Bu Sun Kim | 8496ebe | 2019-08-12 18:04:35 -0700 | [diff] [blame] | 112 | ```python |
Grant Timmerman | 5c11b0a | 2019-06-26 07:39:48 -0700 | [diff] [blame] | 113 | from google.oauth2 import service_account |
| 114 | import googleapiclient.discovery |
| 115 | |
| 116 | SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin'] |
| 117 | SERVICE_ACCOUNT_FILE = '/path/to/service.json' |
| 118 | |
| 119 | credentials = service_account.Credentials.from_service_account_file( |
| 120 | SERVICE_ACCOUNT_FILE, scopes=SCOPES) |
| 121 | sqladmin = googleapiclient.discovery.build('sqladmin', 'v1beta3', credentials=credentials) |
| 122 | response = sqladmin.instances().list(project='exemplary-example-123').execute() |
| 123 | |
| 124 | print(response) |
Liron Newman | fb6fcc8 | 2020-04-21 18:53:01 +0100 | [diff] [blame] | 125 | ``` |