function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DrceteDrcete 

Generate oauth token programatically?

Hello
Is it possible to generate an OAuth access token programmatically (as it's generated with the username-password flow) for a user that performs certain action from a visualforce page?
Where are these tokens stored?
I need to send this token to an external service to perform an API call.
I'm trying to avoid making the user type his username/password again.
AbhishekAbhishek (Salesforce Developers) 
You can't programmatically generate the OAuth token in Salesforce. You have two options here: 


Use the username-password flow to get the token. Using then user don't need to verify again on screen. But this will be the same for all users and also you might need a dummy user for this.

Second, you can use the JWT token to authenticate the user and run the flow. I prefer this over the other.

For reference check this too,
https://help.salesforce.com/articleView?id=sf.remoteaccess_oauth_jwt_flow.htm&type=5


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.