What This Adds In Cloud Code
- Reuses OAuth2 integrations configured at site level.
- Simplifies authorization code and client credentials flows.
- Returns clean token string for downstream API calls.
Quick Start
js
const OAuth2 = require('oauth2');
const url = OAuth2.authorizeUrl('auth_code', { redirect_uri: 'https://example.com/callback' });API
| Function | Params | Returns | Notes |
|---|---|---|---|
authorize_url(grantType, options) / authorizeUrl(...) | grant + options | string | null | Works for auth_code grant only. |
get_token(grantType, options) / getToken(...) | grant + options | string | null | Supports auth_code and client_credentials. |
Practical Example
js
const OAuth2 = require('oauth2');
const HTTP = require('http');
const accessToken = OAuth2.getToken('client_credentials', {
scope: 'contacts:read contacts:write'
});
if (!accessToken) throw new Error('OAuth token unavailable');
const result = HTTP.get('https://partner.example.com/v1/contacts', { Authorization: `Bearer ${accessToken}` });Failure Modes & Gotchas
- Invalid grant type returns
null. - Missing site integration returns
null. - Handle callback state/PKCE explicitly in your route logic if required.