Understanding the Invalid Grant Authentication Failure
The "error": "invalid_grant", "error_description": "authentication failure"
error occurs during OAuth authentication when Salesforce is unable to validate your credentials or authenticate your connection request. This typically happens when using OAuth flows to connect external applications with Salesforce.
Causes of the Invalid Grant Authentication Failure
-
Incorrect User Permissions: The Connected App may not have the proper permissions configured. The OAuth settings might not have "Allow OAuth Username-Password Flows" enabled, which is critical for integration between systems. Even if IP relaxation and authorization are set up correctly, the authentication will fail without this setting.
-
Missing or Incorrect Security Token: When authenticating with the password grant type, you might need to append your security token to your password. Sometimes the system needs only the password and not the security token, or you might be using the wrong security token.
-
URL or Endpoint Issues: Using the wrong Salesforce domain in your OAuth request can cause authentication failures. Salesforce has different OAuth endpoints, and you need to ensure you're using the correct one based on whether you're connecting to a production org, sandbox, or custom domain.
-
IP Restrictions: Authentication failures can occur when there are IP restrictions or login hour restrictions in place for the user account you're trying to authenticate with.
How to Handle the Invalid Grant Authentication Failure
-
Update Connected App Settings: Change the OAuth Policy Permitted Users setting to "All users may self-authorize" in your Connected App. Navigate to Setup > App Manager > [your Connected App name] > click the dropdown arrow > Manage > Edit Policies. Also, ensure "Allow OAuth Username-Password Flows" is enabled.
-
Check Security Token and Password: Make sure you're correctly including the security token with your password when required. In some cases, you may need to use
--data-urlencode
instead of-d
in your cURL requests to properly encode special characters in passwords. -
Verify Endpoint URLs: Ensure you're using the correct Salesforce domain in your OAuth requests. The primary endpoints use
login.salesforce.com
, but you might need to use your My Domain, community domain, ortest.salesforce.com
(for sandboxes) depending on your setup. -
Check IP Restrictions: Verify there are no IP restrictions for the user or Connected App. You can set the IP Relaxation connected app setting to "Relax IP Restrictions" to resolve this issue.
Conclusion
The "invalid_grant, authentication failure" error in Salesforce OAuth is often resolved by adjusting Connected App settings, verifying security tokens, using correct endpoints, and ensuring proper encoding of credentials. By systematically checking these common causes, you can establish successful connections between your application and Salesforce using OAuth authentication.