OAuth2 browser-based authorization for Google APIs.
Opens the user's default browser to Google's consent screen, then listens
on a random port on 127.0.0.1 for the authorization code callback. No
external CLI tool (gcloud, etc.) is required — only a browser.
Each sign-in sends a random state and a PKCE (S256) code challenge. A
callback whose state does not match is answered with 400 and ignored, and
the code is exchanged together with the PKCE verifier, so only this process
can redeem it.
OAuth client registration
This module ships with placeholder client credentials. Before authorize/1
will work you must register a Google OAuth "Desktop app" client:
- Go to https://console.cloud.google.com/apis/credentials
- Click Create credentials → OAuth client ID
- Application type: Desktop app — name it
mob_dev CLI - Click Create — note the Client ID and Client secret
- Fill in
@default_client_idand@default_client_secretin this file
For installed CLI tools, the client_secret is not actually secret — this follows Google's documented guidance for desktop applications (the same model used by the gcloud CLI). The token is only obtainable by a user who explicitly grants consent via their browser.
You can also override with environment variables:
GOOGLE_OAUTH_CLIENT_ID / GOOGLE_OAUTH_CLIENT_SECRET.
Summary
Functions
Runs the browser-based OAuth2 flow and returns a bearer access token.
Builds the Google OAuth2 authorization URL, carrying the request's state
and PKCE code challenge.
The PKCE S256 code challenge for verifier (RFC 7636 §4.2).
Generates the per-sign-in secrets: a random state and a PKCE verifier
with its S256 challenge.
Parses an OAuth callback HTTP request line against the expected state.
Returns the scopes requested during the setup wizard OAuth flow.
Types
Functions
Runs the browser-based OAuth2 flow and returns a bearer access token.
Opens the user's browser to the Google consent screen, then waits up to
timeout_ms milliseconds (default 120 000) for the callback redirect.
Options:
:scopes— list of OAuth scope strings (required):timeout_ms— callback wait timeout in ms (default: 120_000)
Returns {:ok, access_token} or {:error, reason}.
Builds the Google OAuth2 authorization URL, carrying the request's state
and PKCE code challenge.
Pure function — useful for testing and for displaying the URL in case the automatic browser open fails.
The PKCE S256 code challenge for verifier (RFC 7636 §4.2).
@spec new_request() :: request()
Generates the per-sign-in secrets: a random state and a PKCE verifier
with its S256 challenge.
@spec parse_callback_request(String.t(), String.t()) :: {:ok, String.t()} | {:error, String.t()} | {:ignore, String.t()}
Parses an OAuth callback HTTP request line against the expected state.
The request line has the form:
GET /callback?state=STATE&code=AUTH_CODE&scope=... HTTP/1.1Returns {:ok, code}, {:error, reason} when Google reports an error for
this sign-in, or {:ignore, reason} when the request is not this sign-in's
callback (wrong or missing state, another path, unparseable). An ignored
request never yields a code.
@spec setup_scopes() :: [String.t()]
Returns the scopes requested during the setup wizard OAuth flow.