# Cloud projects and OAuth setup


`gclientid` splits provisioning into composable steps: preset and configuration helpers, browser-driven Cloud Console OAuth setup, and REST calls for Resource Manager and Service Usage.

## Presets and configuration

[`Preset`](api/oauth.html#preset) pairs OAuth scopes with the Google APIs they require, and `+` unions two presets in order. Built-in presets (`GOOGLE_APPS`, `GMAIL`, `IDENTITY`, `COLAB`, `CLOUD`, `WORKSPACE_ADMIN`, `WORKSPACE_ADDON`, `MAX`) are available in `PRESETS`.

### Preset

``` python
def Preset(
    scopes:tuple=(), apis:tuple=()
):
```

*OAuth scopes and the Google APIs they need; `+` unions two presets in order*

### oauth_config

``` python
def oauth_config(
    preset:str='google-apps', scopes=None, apis=None
)->gclientid.oauth.Preset:
```

*The preset’s scopes and APIs plus any additions, deduplicated*

### oauth_settings

``` python
def oauth_settings(
    path:pathlib._local.Path=None, create:bool=False, internal:bool=False
):
```

*Load gclientid’s provisioning settings*

### project_id

``` python
def project_id(
    owner:str, # Email of the Google account that owns the project
    internal:bool=False, # The separate Internal-audience project?
):
```

*Stable default project ID for `owner`; Google requires global uniqueness, so it hashes the email*

### config_dir

``` python
def config_dir():
```

### output_dir

``` python
def output_dir(
    path:pathlib._local.Path=None
):
```

## Cloud Console OAuth setup

Connect to a signed-in Chrome session with [`connect_browser`](api/oauth.html#connect_browser), then configure the OAuth app, create a Web or Desktop client, register redirect URIs, and run local OAuth authorization with PKCE.

``` python
cdp, page = await connect_browser()
await configure_app(page, project_id, internal=True, support_email='me@example.com')
await create_client(page, project_id, 'oauth-client-internal.json')
await add_redirects(page, 'oauth-client-internal.json', ['http://localhost:3000/redirect'])
```

### connect_browser

``` python
async def connect_browser(
    default_browser:bool=True, # Use normal Chrome instead of CDP Chrome?
    timeout:int=60, # Seconds to wait for normal Chrome's approval
)->tuple[fastcdp.core.CDP, fastcdp.core.Page]:
```

*Connect to Chrome and return its connection and a new page*

### console_account

``` python
async def console_account(
    page:fastcdp.core.Page, timeout:int=10
)->str:
```

*Email of the Google account signed into the Cloud Console*

### configure_app

``` python
async def configure_app(
    page:fastcdp.core.Page, # Signed-in Google Cloud Console page
    project_id:str, # Existing Google Cloud project ID
    name:str='gclientids', # OAuth application name
    scopes:tuple=('openid', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://mail.google.com/', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/contacts', 'https://www.googleapis.com/auth/contacts.other.readonly', 'https://www.googleapis.com/auth/directory.readonly', 'https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.orgunit', 'https://www.googleapis.com/auth/admin.directory.domain', 'https://www.googleapis.com/auth/admin.directory.resource.calendar', 'https://www.googleapis.com/auth/admin.directory.rolemanagement', 'https://www.googleapis.com/auth/admin.reports.audit.readonly', 'https://www.googleapis.com/auth/admin.reports.usage.readonly', 'https://www.googleapis.com/auth/apps.licensing', 'https://www.googleapis.com/auth/colaboratory'), # Scopes to declare on the Data Access page
    internal:bool=False, # Restrict OAuth authorization to the Cloud project's organization?
    support_email:str=None, # Require this support/contact email in the signed-in Console session
    accept_terms:bool=True, # Accept Google's API Services terms without pausing?
    timeout:int=10, # Seconds to wait for each Console operation
    terms_timeout:int=600, # Seconds to wait while the developer handles the terms screen
):
```

*Idempotently configure the project’s OAuth app: audience, branding, declared scopes, and publication*

### create_client

``` python
async def create_client(
    page:fastcdp.core.Page, # Signed-in Google Cloud Console page
    project_id:str, # Google Cloud project ID whose OAuth app `configure_app` has set up
    path:str | pathlib._local.Path='oauth-client.json', # Destination for Google's client JSON
    name:str='gclientids', # OAuth client name
    desktop:bool=False, # Create a Desktop client instead of a Web client?
    redirects:tuple=('http://127.0.0.1:53682/', 'https://oauth.appapis.org/redirect', 'http://localhost:5001/redirect', 'http://127.0.0.1:5001/redirect', 'http://localhost:5002/redirect', 'http://127.0.0.1:5002/redirect', 'http://localhost:8000/redirect', 'http://127.0.0.1:8000/redirect', 'http://localhost:8080/redirect', 'http://127.0.0.1:8080/redirect'), # Authorized redirect URIs of a Web client
    timeout:int=10, # Seconds to wait for each Console operation
)->dict:
```

*Create an OAuth client and save its client JSON, in Google’s `web` or `installed` shape*

### add_redirects

``` python
async def add_redirects(
    page:fastcdp.core.Page, # Signed-in Google Cloud Console page
    path:str | pathlib._local.Path, # Stored Web client JSON
    redirects:tuple=('http://127.0.0.1:53682/', 'https://oauth.appapis.org/redirect', 'http://localhost:5001/redirect', 'http://127.0.0.1:5001/redirect', 'http://localhost:5002/redirect', 'http://127.0.0.1:5002/redirect', 'http://localhost:8000/redirect', 'http://127.0.0.1:8000/redirect', 'http://localhost:8080/redirect', 'http://127.0.0.1:8080/redirect'), # Redirect URIs that must be registered
    timeout:int=10, # Seconds to wait for each Console operation
)->list:
```

*Register any of `redirects` the Web client lacks, in Google and in its JSON; returns the registered list*

### authorize_google

``` python
async def authorize_google(
    client_path:str | pathlib._local.Path='oauth-client.json', # Web or Desktop client JSON from create_client
    token_path:str | pathlib._local.Path='oauth-token.json', # Destination for access and refresh token JSON
    preset:str='google-apps', # Scope preset
    scopes=None, # Additional OAuth scopes
    account:str=None, # Google account email hint and verification
    cdp:fastcdp.core.CDP=None, # Existing CDP connection; the default browser if omitted
    remote:bool=False, # Use appapis copy/paste instead of a local callback (Web clients only)?
    open_browser:bool=True, # Open the appapis authorization URL in the default browser?
)->dict:
```

*Authorize Google APIs and save refreshable token JSON*

For finer control than [`configure_app`](api/oauth.html#configure_app), call the individual consent-screen steps directly.

### setup_auth

``` python
async def setup_auth(
    page:fastcdp.core.Page, # Signed-in Google Cloud Console page
    project_id:str, # Existing Google Cloud project ID
    name:str='gclientids', # OAuth application name
    internal:bool=False, # Restrict authorization to the project's organization?
    support_email:str=None, # Require this support/contact email in the signed-in Console session
    accept_terms:bool=True, # Accept Google's API Services terms without pausing?
    timeout:int=10, # Seconds to wait for each Console operation
    terms_timeout:int=600, # Seconds to wait for manual terms acceptance
):
```

*Initialize the OAuth app’s name, audience, and contact details unless it already exists*

### set_branding

``` python
async def set_branding(
    page:fastcdp.core.Page, project_id:str, timeout:int=10
):
```

*Set gclientid’s homepage, privacy URL, and authorized domain; return whether anything changed*

### set_scopes

``` python
async def set_scopes(
    page:fastcdp.core.Page, project_id:str,
    scopes:tuple=('openid', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://mail.google.com/', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/contacts', 'https://www.googleapis.com/auth/contacts.other.readonly', 'https://www.googleapis.com/auth/directory.readonly', 'https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.orgunit', 'https://www.googleapis.com/auth/admin.directory.domain', 'https://www.googleapis.com/auth/admin.directory.resource.calendar', 'https://www.googleapis.com/auth/admin.directory.rolemanagement', 'https://www.googleapis.com/auth/admin.reports.audit.readonly', 'https://www.googleapis.com/auth/admin.reports.usage.readonly', 'https://www.googleapis.com/auth/apps.licensing', 'https://www.googleapis.com/auth/colaboratory'),
    timeout:int=10
):
```

*Add missing declared scopes without removing existing scopes; return the added scopes*

### publish_app

``` python
async def publish_app(
    page:fastcdp.core.Page, project_id:str, timeout:int=10
):
```

*Publish an External OAuth app unless it is already in production; do not use for Internal apps*

## Cloud projects and APIs

These REST helpers create, find, and soft-delete Google Cloud projects, grant project IAM roles, and enable Google APIs through Service Usage using an owner’s stored Cloud credentials.

``` python
await provision_project(
    'me@example.com', project_id, name='gclientids', domain='example.com',
    apis=['cloudresourcemanager.googleapis.com'])
await delete_project('me@example.com', project_id)
```

### provision_project

``` python
async def provision_project(
    account, project_id, name=None, domain=None, apis:tuple=()
):
```

*Create an optionally organization-owned project and enable its APIs*

### create_project

``` python
async def create_project(
    creds, project_id, name=None, parent=None
):
```

*Create a Google Cloud project through Resource Manager*

### find_project

``` python
async def find_project(
    creds, project_id
):
```

*Find an accessible Cloud project by its globally unique project ID*

### delete_project

``` python
async def delete_project(
    account, project
):
```

*Soft-delete a Cloud project through Resource Manager*

### enable_apis

``` python
async def enable_apis(
    creds, project, apis
):
```

*Enable only missing Google APIs through Service Usage, returning the newly enabled names*

### enabled_apis

``` python
async def enabled_apis(
    creds, project
):
```

*Service names currently enabled on a project*

### find_organization

``` python
async def find_organization(
    creds, domain
):
```

*Find the single visible Google Cloud organization for a Workspace domain*

### grant_project_roles

``` python
async def grant_project_roles(
    creds, project, member, roles:tuple=('roles/serviceusage.serviceUsageConsumer',)
):
```

*Idempotently grant project IAM roles to a member*

### cloud_creds

``` python
async def cloud_creds(
    account
):
```

*Load the owner’s stored token, which must carry Google Cloud access*

## Helpers

### ensure_project_ui

``` python
async def ensure_project_ui(
    page:fastcdp.core.Page, project_id:str, name:str=None, timeout:int=30
):
```

*Create [`project_id`](api/config.html#project_id) through the Console unless it already exists*

### create_project_ui

``` python
async def create_project_ui(
    page:fastcdp.core.Page, project_id:str, name:str=None, timeout:int=30
):
```

*Create a Google Cloud project through its Console*

### project_exists_ui

``` python
async def project_exists_ui(
    page:fastcdp.core.Page, project_id:str, timeout:int=10
):
```

*Whether the signed-in Console account can open [`project_id`](api/config.html#project_id)*
