Overview
Postiz uses OAuth 2.0 to securely connect user accounts from various social media platforms. Each integration implements a standard OAuth flow with platform-specific variations.OAuth 2.0 Flow
Standard Authorization Code Flow
Implementation
1. Generate Authorization URL
ThegenerateAuthUrl() method creates the OAuth URL:
state: Random string to prevent CSRF attackscodeVerifier: For PKCE (Proof Key for Code Exchange) - adds securityredirect_uri: Where user returns after authorizationscope: Permissions requested from the platform
2. Handle OAuth Callback
The frontend receives the callback and sends code to backend:Frontend: integrations/social/platform/page.tsx
3. Exchange Code for Token
Theauthenticate() method exchanges the code:
4. Token Refresh
Tokens expire and must be refreshed automatically:Scope Validation
Always validate that granted scopes match requested scopes:- User may decline some permissions
- Platform may not support all requested scopes
- Insufficient scopes will cause posting failures
Platform-Specific Variations
Twitter/X OAuth 2.0
LinkedIn OAuth 2.0
Facebook OAuth 2.0
Google/YouTube OAuth 2.0
PKCE (Proof Key for Code Exchange)
PKCE adds security to OAuth flow:Automatic Token Refresh
Postiz uses Temporal workflows to automatically refresh tokens:- Temporal workflow monitors token expiration
- Triggers refresh 1 hour before expiry
- Calls provider’s
refreshToken()method - Updates database with new tokens
- Reschedules next refresh
Error Handling
Handling OAuth Errors
Common OAuth Errors
| Error | Cause | Solution |
|---|---|---|
invalid_grant | Code already used or expired | Get new authorization code |
invalid_client | Wrong client ID/secret | Check environment variables |
redirect_uri_mismatch | Redirect URI doesn’t match | Update OAuth app settings |
access_denied | User declined authorization | Ask user to try again |
invalid_scope | Unsupported scope requested | Review platform documentation |
Security Best Practices
Environment Configuration
.env
Next Steps
Creating Provider
Build a complete integration provider
Testing
Test your OAuth implementation