# Setting up the Policy API

This page explains how to set up the Cloud Identity Policy API before [listing and getting policies](https://docs.cloud.google.com/identity/docs/how-to/list-get-policies.md.txt).

## Install the Python client library

To install the Python client library, run the following command:

```bash
  pip install --upgrade google-api-python-client google-auth \
    google-auth-oauthlib google-auth-httplib2 absl-py
```

For more on setting up your Python development environment, refer to the
[Python Development Environment Setup Guide](https://docs.cloud.google.com/python/docs/setup.md.txt).

## Enable the API and set up service account credentials

<br />

## Authenticate as a service account with domain-wide delegation

If you're an administrator managing identity policies, or if you want to provide
an account with domain-wide privileges so that it can manage Google policies on
behalf of administrators, you should authenticate as a
[service account](https://docs.cloud.google.com/iam/docs/service-accounts) and then grant domain-wide
privileges to the service account.

> [!NOTE]
> **Note:** Because domain-wide delegation lets the service account impersonate an administrator user, service account actions are logged as having been done by the user.

For details about setting up domain-wide delegation, see
[Control API access with domain-wide delegation](https://support.google.com/a/answer/162106).
Review the [best practices](https://support.google.com/a/answer/14437356) to
mitigate the security risks associated with using domain-wide delegation.

After setting up domain wide delegation, [Application Default Credentials (ADC)](https://docs.cloud.google.com/docs/authentication/application-default-credentials.md.txt) can be used for authentication. When you use
ADC, your code can run in either a development or production environment without
changing how your application authenticates to Google Cloud services and APIs.
When initializing the credentials in your code, specify the email address that
the service account acts on by using the `subject()` parameter on the
credential. Make sure that the email address is granted the
`Service Account User` role on the service account (as described above).
For example:
> **Important:** The OAuth scopes specified in your application code used
> to create delegated credentials must be present in the authorized scopes
> list for domain-wide delegation in the Google Admin Console. A broader
> or more permissive scope won't work. If a scope requested by the application to create delegated credentials is not authorized in
> domain-wide delegation, the application receives an `unauthorized_client`
> error.

### Python

```python
AUTH_SCOPES = ['https://www.googleapis.com/auth/iam']
# The read and write scope of the API. Note that you must authorize the
# exact same scope for domain-wide delegation in the Google Admin Console.
POLICY_SCOPES = ['https://www.googleapis.com/auth/cloud-identity.policies']
TOKEN_URI = "https://accounts.google.com/o/oauth2/token"

_ADMIN_EMAIL = flags.DEFINE_string(
    name='admin_email',
    default=None,
    help='Administrator email to call as',
    required=True,
)

# Fetch application default credentials (ADC)
credentials, _ = google.auth.default(scopes=AUTH_SCOPES)

# Populate account information
request = requests.Request()
credentials.refresh(request)

# Create an IAM signer
signer = iam.Signer(request, credentials,
                    credentials.service_account_email)

# Create domain-wide delegated (DWD) credentials
delegated_credentials = service_account.Credentials(
    signer=signer,
    service_account_email=credentials.service_account_email,
    token_uri=TOKEN_URI,
    scopes=POLICY_SCOPES,
    subject=_ADMIN_EMAIL.value
)
```

In order to impersonate a service account when using Application default
credentials, use the `impersonate-service-account` flag.

### Shell

```bash
gcloud auth application-default login --impersonate-service-account=<service_account_email>
--scopes=https://www.googleapis.com/auth/iam,https://www.googleapis.com/auth/cloud-identity.policies
```

Detailed sample code to call Policy API, including the code for
authentication, are provided in [Listing and getting policies](https://docs.cloud.google.com/identity/docs/how-to/list-get-policies.md.txt).