Client Credentials Flow
The Client Credentials flow is used for server-to-server authentication where the application acts on its own behalf rather than on behalf of a user. This flow is ideal for machine-to-machine (M2M) communication.Overview
This flow is designed for backend services, daemons, and other applications that need to authenticate themselves to access resources. Unlike user-centric flows, there is no user authentication step—the application uses its own credentials to obtain an access token.Flow Steps
- Token Request: The application authenticates with its client credentials
- Token Response: The authorization server returns an access token
- Access Resources: Use the access token to access protected resources
Implementation
Prerequisites
Before starting, you’ll need:- Your application’s
client_idandclient_secret - The token endpoint:
https://login.camall.io/oauth/token - Required scopes for your application
Step 1: Initiate Token Request
Request an access token directly from the token endpoint using your client credentials:grant_type=client_credentials: Specifies the client credentials flowclient_id: Your application’s client IDclient_secret: Your application’s client secretscope: Requested permissions (space-separated, optional)
Step 2: Exchange Credentials for Access Token
The token endpoint responds with an access token: Response:access_token: The JWT token used to authenticate API requeststoken_type: Always “Bearer” for OAuth 2.0expires_in: Token lifetime in seconds (e.g., 3600 = 1 hour)scope: Granted scopes (may differ from requested scopes)
Step 3: Use the Access Token
Make authenticated API requests using the access token:Complete Example Script
Here’s a complete bash script demonstrating the entire flow:Complete Example Script (PowerShell)
For Windows users, here’s a PowerShell version:Security Best Practices
- Secure client secret: Store client secrets securely (environment variables, key vaults, secrets managers)
- Use HTTPS: All endpoints must use HTTPS in production
- Rotate credentials: Regularly rotate client IDs and secrets
- Limit scope: Request only the minimum scopes required for your application
- Monitor token usage: Track token generation and API calls for anomalies
- Short-lived tokens: Tokens should have limited lifetime (typically 1 hour)
- Network restrictions: Consider IP allowlisting for additional security
- Never expose secrets: Never commit secrets to version control or client-side code
HTTP Request Examples
Raw HTTP Request - Token Request (POST form)
Raw HTTP Request - Token Request (Basic Auth)
Raw HTTP Request - API Call with Bearer Token
Common Use Cases
The Client Credentials flow is ideal for:- Backend services: Microservices communicating with each other
- Scheduled jobs: Cron jobs or scheduled tasks that need API access
- Data processing: Batch processing or ETL operations
- System integrations: Third-party system integrations
- CLI tools: Command-line tools for administrative tasks
- Monitoring services: Health checks and monitoring systems
Error Handling
Handle common errors when using the Client Credentials flow:Comparison with Other Flows
| Feature | Client Credentials | Authorization Code |
|---|---|---|
| User interaction | None | Required |
| Use case | Machine-to-machine | User authentication |
| Refresh token | No | Yes |
| PKCE required | No | Recommended |
| Client secret | Required | Required |
| Redirect URI | Not used | Required |