How to fix "invalid_grant" and "invalid authorization code" error on Salesforce

When you want to integrate Salesforce into your own projects, you can use the Saleforce API's to send and receive data between Salesforce and your own project. This "invalid_grant" and "invalid authorization code" usually occurs while you are trying to generate the access token - and in the blog, we will look at how to fix this error.

Fixing the "invalid_grant" and "invalid authorization code" Salesforce error:

{

"error": "invalid_grant",

"error_description": "invalid authorization code"

}
  1. First make sure that you have enabled API access in the Salesforce settings page.
  2. Then check the exact authorization code that is generated. Sometimes the authorization code will be generated along with null or escape characters which needs to be replaced. E.g. Replace "%3D%3D" with "==".
  3. If you are using Postman to make requests, check the request and make sure there are no errors on it
  4. If this does not work, then you need to debug this further by testing the Salesforce APIs locally. The easiest way to do this is by forking the official Salesforce API project from Postman to your local system - and test the access token and authorization flow locally with environment variables set up.