Generating Tokens
Generating Tokens
Learn how to generate access tokens using different OAuth2 flows and configurations.
Quick Reference
| Task | Command |
|---|---|
| Get token with profile | entratool get-token -p PROFILE |
| Override scope | entratool get-token -p PROFILE --scope "SCOPE" |
| Specify flow | entratool get-token -p PROFILE -f FLOW |
| Silent mode | entratool get-token -p PROFILE --silent |
| Save to file | entratool get-token -p PROFILE -o token.txt |
| Refresh token | entratool refresh -p PROFILE |
Basic Token Generation
Using a Profile
| |
Output:
Authenticating with profile 'my-profile'...
✓ Token acquired successfully
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ij...What happens:
- Profile loaded from
~/.entratool/profiles.json - Secrets retrieved from secure storage
- Authentication flow executed
- Access token printed to stdout
Specifying a Flow
Override the default flow:
| |
Available flows:
ClientCredentialsAuthorizationCodeDeviceCodeInteractiveBrowser
Client Credentials Flow
Non-Interactive Service Authentication
| |
Requirements:
- Client secret or certificate configured
- Application permissions granted
- Admin consent completed
Example:
| |
Output:
✓ Token acquired successfully
eyJ0eXAiOiJKV1QiLCJh...Interactive Browser Flow
User Authentication with Browser
| |
What happens:
- Browser opens to Entra ID login page
- User enters credentials
- User consents to permissions (if required)
- Browser redirects to localhost with code
- Tool exchanges code for token
Example:
| |
Output:
Opening browser for authentication...
✓ Authentication successful
✓ Token acquired
eyJ0eXAiOiJKV1QiLCJh...Device Code Flow
Authentication on Limited-Input Devices
| |
What happens:
- Tool displays code and URL
- User visits URL on another device
- User enters code
- User authenticates
- Tool polls and receives token
Example:
| |
Output:
Device Code Authentication
To sign in, use a web browser to open:
https://microsoft.com/devicelogin
and enter the code: ABCD-1234
Waiting for authentication...
✓ Token acquired successfullyAuthorization Code Flow
Web Application Authentication
| |
Requirements:
- Redirect URI configured in app registration
- Delegated permissions
- User credentials
Example:
| |
Output:
Opening browser for authentication...
Redirect URI: http://localhost:8080
✓ Authorization code received
✓ Token acquired
eyJ0eXAiOiJKV1QiLCJh...Scope Override
Runtime Scope Specification
Override profile’s default scope:
| |
Use cases:
- Different operations need different permissions
- Testing with minimal scopes
- Temporary scope changes
Multiple Scopes
Space-separated:
| |
Comma-separated:
| |
.default Scope
Request all configured permissions:
| |
Certificate Authentication
Using Certificates Instead of Secrets
| |
Profile configuration:
| |
Certificate password:
- Stored securely in platform-specific storage
- Prompted once during profile creation
- Never stored in plaintext
Advantages
✅ More secure than secrets
✅ Longer validity periods
✅ Better for automation
✅ Certificate rotation support
Output Options
Silent Mode
Suppress all output except the token:
| |
Output:
eyJ0eXAiOiJKV1QiLCJh...Use in scripts:
| |
Save to File
| |
Output:
✓ Token saved to token.txtUse later:
| |
JSON Output
| |
Output:
| |
Token Inspection
Inspect Token Claims
| |
Or separately:
| |
Output:
| |
Learn about token inspection →
Token Refresh
Refreshing Expired Tokens
If you have a refresh token:
| |
Requirements:
- Original token acquired with
offline_accessscope - Refresh token stored in cache
Output:
Refreshing token for profile 'myprofile'...
✓ Token refreshed successfully
eyJ0eXAiOiJKV1QiLCJh...Note: Client Credentials flow doesn’t support refresh tokens. Just request a new token instead.
Learn about token refreshing →
Common Patterns
Pattern 1: Script Automation
| |
Pattern 2: Environment Variable
| |
Pattern 3: Token Caching
| |
Pattern 4: Multi-API Access
| |
Troubleshooting
“Profile not found”
Cause: Profile name is incorrect
Fix:
| |
“AADSTS70011: Invalid scope”
Cause: Requested scope is not configured in app registration
Fix:
- Go to Azure Portal → App registrations
- Select your app → API permissions
- Add the required permission
- Grant admin consent (if needed)
“AADSTS7000215: Invalid client secret”
Cause: Client secret is expired or incorrect
Fix:
| |
“Insufficient privileges”
Cause: Token has scope but lacks underlying permission
Fix:
- Verify API permissions in Azure Portal
- Grant admin consent
- For users: Assign appropriate Azure AD role
“Browser did not respond”
Cause: Browser authentication timed out
Fix:
- Try Device Code flow instead:
1entratool get-token -p myprofile -f DeviceCode - Check redirect URI configuration
- Ensure localhost port is not blocked
Best Practices
✅ Use Appropriate Flows
- Automation: Client Credentials
- User apps: Interactive Browser
- Headless/SSH: Device Code
- Web apps: Authorization Code
✅ Request Minimal Scopes
| |
✅ Handle Token Expiration
| |
✅ Secure Token Storage
| |
❌ Avoid
- ❌ Hard-coding tokens
- ❌ Committing tokens to git
- ❌ Sharing tokens via email/Slack
- ❌ Using expired tokens
- ❌ Requesting excessive scopes