Sometimes need to get a quick bearer token to test an API in postman or python. Rather than going through the tedious process of setting up an App or fiddling with OAuth, the salesforce cli tool can be used to get a token to use in API queries.
First step is authenticating, this pops up a web-page where you can SSO-login or password login to salesforce. On login, execute the second command to get a token.
PS C:\> sfdx force:auth:web:login
» Warning: sfdx-cli update available from 7.182.1 to 7.191.1.
Successfully authorized keith@domain.com with org ID XXXXXXXXXXXXXX
PS C:\> sfdx force:org:display --targetusername keith@domain.com
» Warning: sfdx-cli update available from 7.182.1 to 7.191.1.
WARNING: This command will expose sensitive information that allows for subsequent activity using your current authenticated session.
Sharing this information is equivalent to logging someone in under the current credential, resulting in unintended access and escalation of privilege.
For additional information, please review the authorization section of the https://developer.salesforce.com/docs/atlas.en-us.234.0.sfdx_dev.meta/sfdx_dev/sfdx_dev_auth_web_flow.htm
=== Org Description
KEY VALUE
──────────────── ────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Access Token YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
Client Id PlatformCLI
Connected Status Connected
Id XXXXXXXXXXXXXX
Instance Url https://somcompany.my.salesforce.com
Username keith@domain.com
Extract the Access Token, and you’re in business. Bear in mind the token expires in a few hours typically.