README
¶
Client Credentials Example
This example demonstrates how to use the PingOne Go Client SDK with OAuth2 Client Credentials flow for server-to-server authentication.
Use Case
Client Credentials flow is ideal for:
- Server-to-server authentication
- Machine-to-machine (M2M) communication
- Backend services and APIs
- Automation scripts and batch jobs
- Applications that don't require user interaction
PingOne Configuration
1. Create a Worker Application in PingOne
- Log in to your PingOne Admin Console
- Navigate to Applications
- Click + Application
- Select Worker
- Provide a name (e.g., "Go SDK Client Credentials Example")
- Click Save
2. Note Your Credentials
IMPORTANT: Save these credentials securely - the client secret is shown only once:
- On the application's Configuration tab, copy the Client ID
- Copy the Client Secret (shown only once during creation)
- You'll use these in the environment variables below
3. Assign Roles
Worker applications can have the same roles as human administrators. Grant necessary permissions:
- Go to the Roles tab in your application
- Click Grant Roles
- Select the environment(s) where you want to grant access
- Assign appropriate roles based on your needs:
- Environment Admin: Full access to the environment
- Identity Data Admin: User and group management
- Client Application Developer: Application management
- Identity Data Read Only: Read-only access to identity data
- Or other specific roles as needed
- Click Save
Running the Example
Set Environment Variables
export PINGONE_CLIENT_ID="your-client-id-here"
export PINGONE_CLIENT_SECRET="your-client-secret-here"
export PINGONE_ENVIRONMENT_ID="your-environment-id-here"
export PINGONE_ROOT_DOMAIN="pingone.com" # or pingone.eu, pingone.asia
Security Note: Never commit these credentials to version control. Consider using:
- Environment variable files (
.env) that are.gitignored - Secret management systems (AWS Secrets Manager, HashiCorp Vault, etc.)
- CI/CD platform secret storage
Run the Example
cd examples/basic
go run main.go
What to Expect
- Authentication: The SDK automatically authenticates using the client credentials
- Token Acquisition: An access token is obtained from PingOne
- API Call: The example retrieves environment details
- Output: Environment information is displayed in the console:
Successfully read environment id=12345678-1234-1234-1234-123456789abc name=My Environment type=SANDBOX region=NA
How It Works
The client credentials flow:
- Application sends client ID and secret to PingOne token endpoint
- PingOne validates the credentials
- PingOne returns an access token
- Application uses the token for API calls
- Token is automatically refreshed when it expires
Troubleshooting
"Invalid client" error:
- Verify your
PINGONE_CLIENT_IDandPINGONE_CLIENT_SECRETare correct - Ensure the application exists in the specified environment
- Check that the application is enabled
"Insufficient privileges" error:
- The application needs appropriate roles assigned
- Go to your PingOne application's Roles tab and grant necessary permissions
- Different API operations require different roles
"Environment not found" error:
- Verify your
PINGONE_ENVIRONMENT_IDis correct - Ensure the Worker App has access to this environment
- Check that roles are granted for the specific environment
Connection errors:
- Verify
PINGONE_ROOT_DOMAINmatches your PingOne region - Check your network connectivity
- Ensure firewall rules allow outbound HTTPS traffic
Workflow Diagram
┌─────────────┐ ┌──────────────┐
│ Worker │ │ PingOne │
│ App │ │ Server │
└──────┬──────┘ └──────┬───────┘
│ │
│ 1. Request access token │
│ (client_id + client_secret) │
├──────────────────────────────────────────────>│
│ │
│ 2. Validate credentials │
│ │
│ 3. Return: access_token │
│<──────────────────────────────────────────────┤
│ │
│ 4. Use token for API calls │
├──────────────────────────────────────────────>│
│ │
│ 5. Return: API response │
│<──────────────────────────────────────────────┤
│ │
│ 6. Token expires, request new token │
│ (automatic refresh) │
├──────────────────────────────────────────────>│
│ │
│ 7. Return: new access_token │
│<──────────────────────────────────────────────┤
Security Best Practices
- Protect Client Secret: Treat it like a password - never commit to source control
- Rotate Credentials: Regularly rotate client secrets
- Least Privilege: Grant only the minimum required roles
- Secure Storage: Use secure secret management systems in production
- Audit Access: Monitor API usage through PingOne audit logs
- Environment Isolation: Use separate Worker Apps for dev/test/prod environments
Additional Resources
Click to show internal directories.
Click to hide internal directories.