Get in Touch With Us

Submitting the form below will ensure a prompt response from us.

When integrating Salesforce with external applications or APIs, developers often encounter authentication errors. One of the most common OAuth-related issues is the “invalid_grant” error. This error can be confusing because it doesn’t always pinpoint a single cause — it simply indicates that Salesforce rejected the authentication grant.

In this article, we’ll explore what the Salesforce Invalid_Grant error means, its common causes, how to fix it, and preventive measures to ensure seamless integration in the future.

What is the Salesforce Invalid_Grant Error?

The invalid_grant error in Salesforce occurs when an OAuth 2.0 token request fails because the credentials are invalid or expired.

When you try to obtain an access token from Salesforce’s OAuth endpoint (usually /token), Salesforce verifies the refresh token, authorization code, or user credentials. If any part of this process fails validation, it returns:

{
  "error": "invalid_grant",
  "error_description": "authentication failure"
}

This means Salesforce couldn’t authorize the request due to a mismatch or expired token, incorrect redirect URI, or user account issues.

When Does the Invalid_Grant Error Occur?

You might see this error during these scenarios:

  1. During OAuth token exchange (authorization code or refresh token).
  2. When using Salesforce connected apps for third-party integration.
  3. While refreshing expired tokens through an API.
  4. When a user’s Salesforce password or security token has changed.
  5. During sandbox refreshes or environment migrations.

7 Common Causes of Invalid_Grant in Salesforce

Here are the most frequent reasons for this error:

Expired or Revoked Refresh Token

Salesforce refresh tokens can expire if:

  • The user revoked app access.
  • The Salesforce admin changed security policies.
  • The user’s password was reset.

Once revoked, the refresh token can no longer generate access tokens.

Solution:
Generate a new refresh token by reauthorizing the connected app or prompting the user to log in again.

Mismatched Redirect URI

The redirect URI specified in your OAuth request must match exactly with the one registered in your Salesforce Connected App settings.

Even small mismatches like missing trailing slashes or HTTP vs HTTPS differences can trigger invalid_grant.

Solution:
Verify that the redirect URI in your code matches the one defined in the Connected App.

Example:

params = {
"grant_type": "authorization_code",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"redirect_uri": "https://yourapp.com/oauth/callback",
"code": AUTH_CODE
}

✅ Ensure https://yourapp.com/oauth/callback exactly matches what’s configured in Salesforce.

Authorization Code Already Used or Expired

OAuth authorization codes in Salesforce are single-use and valid for only 10 minutes. Attempting to reuse or delay using them results in an invalid grant error.

Solution:

  1. Always exchange the authorization code for an access token immediately after it’s generated.
  2. Never reuse a code that has already been redeemed.

Incorrect Username or Password (for Password Grant Type)

When using the password OAuth flow, incorrect credentials will cause an invalid_grant error.

Solution:
Ensure you’re using the correct Salesforce username, password, and security token.
If your Salesforce org uses multi-factor authentication (MFA), switch to another flow such as the JWT Bearer Token Flow.

Sandbox or Environment Refresh

If your Salesforce sandbox has been refreshed, previous credentials, refresh tokens, and connected app authorizations become invalid.

Solution:
Reauthorize the connected app in the refreshed sandbox and obtain new tokens.

IP Restrictions or Network Policies

Salesforce may block token requests from unauthorized IPs or regions if IP restriction policies are enabled in your organization.

Solution:

  1. Whitelist your server IP range in Salesforce Setup → Security Controls → Network Access.
  2. Or disable IP restrictions for the connected app (not recommended for production).

Revoked Connected App Authorization

If a user or admin revoked the connected app from their App Manager → Connected Apps OAuth Usage, your integration will fail.

Solution:
Reauthorize the app by prompting the user to log in again through the OAuth flow.

How to Fix the Salesforce Invalid_Grant Error?

Here’s a checklist to resolve this issue efficiently:

  1. Verify OAuth credentials: Ensure client ID, client secret, and redirect URI are correct.
  2. Reauthorize the connected app: If tokens have expired or been revoked.
  3. Refresh sandbox or environment settings: Reconnect after a Salesforce sandbox refresh.
  4. Check IP addresses and security restrictions; update network access policies as needed.
  5. Use tokens promptly: Don’t reuse or delay the use of authorization codes.
  6. Update environment variables: Ensure your app uses the latest Salesforce credentials.

Sample Python Example Using OAuth 2.0

Below is an example of exchanging an authorization code for an access token using Python and requests:

import requests
TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
payload = {
'grant_type': 'authorization_code',
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET',
'redirect_uri': 'https://yourapp.com/oauth/callback',
'code': 'AUTHORIZATION_CODE'
}

response = requests.post(TOKEN_URL, data=payload)
if response.status_code == 200:
print("Access Token:", response.json()['access_token'])
else:
print("Error:", response.json())

Tip: Always handle token expiration gracefully and refresh tokens securely.

How Moon Technolabs Can Help?

Building and maintaining seamless Salesforce integrations can be complex, especially when handling OAuth flows, token security, and cloud-based synchronization.

At Moon Technolabs, our experts specialize in Salesforce integration, cloud-based automation, and secure API development. We help businesses design robust solutions that minimize authentication errors, such as invalid_grant, and ensure consistent performance across environments.

From token lifecycle management to Salesforce API optimization — Moon Technolabs empowers enterprises with reliable, scalable, and secure cloud integration solutions.

Fix Salesforce Integration Issues with Expert Help

Facing the Salesforce Invalid_Grant error? Our Salesforce specialists can help you resolve authentication issues and ensure reliable API performance.

Talk to Our Salesforce Experts

Conclusion

The Salesforce Invalid_Grant error often occurs due to expired tokens, mismatched redirect URIs, or authorization mismatches. While it can seem frustrating, understanding its causes and implementing structured OAuth management can prevent recurring issues.

By following the best practices outlined above — and leveraging expert Salesforce integration support from Moon Technolabs — you can ensure secure, efficient, and reliable cloud-based application performance without interruption.

About Author

Jayanti Katariya is the CEO of Moon Technolabs, a fast-growing IT solutions provider, with 18+ years of experience in the industry. Passionate about developing creative apps from a young age, he pursued an engineering degree to further this interest. Under his leadership, Moon Technolabs has helped numerous brands establish their online presence and he has also launched an invoicing software that assists businesses to streamline their financial operations.

Related Q&A

bottom_top_arrow

Call Us Now

usa +1 (620) 330-9814
OR
+65
OR

You can send us mail

sales@moontechnolabs.com