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
Andrew LambaAndrew Lamba 

Unable to get API token from OAuth2

I created a Connected App and I set the permitted users to self-authorized.

After trying to get the access token via the curl command, I keep getting the error: {"error":"invalid_grant","error_description":"authentication failure"}

I also requested a new security token. Please note this process worked with my employee email but it isn't working with a service account, if that helps.
Best Answer chosen by Andrew Lamba
AnudeepAnudeep (Salesforce Developers) 
Hi Andrew, 

Here are a few things I recommend checking

1. Check if the IP restrictions are relaxed

User-added image


If not, first, relax IP restrictions for the app.

You can do so by going to Manage Apps -> Connected Apps -> (The name of the app)
Then setting "IP Relaxation" to "Relax IP restrictions".
 
http://stackoverflow.com/questions/34154385/getting-errorinvalid-grant-error-descriptionauthentication-failure-i

2. Second, in your curl command, check if you have left out the security token
 
3. Ensure you have added the Profile of the affected user to the list of App Profiles.

You can do so by going to Manage Apps -> Connected Apps -> (The name of the app)
Then Profiles and "Manage Profiles"

User-added image 
If you followed all of the above and it still does not work, I recommend making a Post request using anonymous apex with the required parameters or using a tool like Postman to see if you are able to recreate the issue and getting additional information in the error message 

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!

All Answers

VinayVinay (Salesforce Developers) 
Hi Andrew,

Check if its pointing to 'https://test.salesforce.com' or 'https://login.salesforce.com' or instance 'na1.salesforce.com'.   Also you probably need to include your API security token with the password.

https://help.salesforce.com/articleView?id=remoteaccess_oauth_flow_errors.htm&type=5

Hope this helps...

Thanks,
AnudeepAnudeep (Salesforce Developers) 
Hi Andrew, 

Here are a few things I recommend checking

1. Check if the IP restrictions are relaxed

User-added image


If not, first, relax IP restrictions for the app.

You can do so by going to Manage Apps -> Connected Apps -> (The name of the app)
Then setting "IP Relaxation" to "Relax IP restrictions".
 
http://stackoverflow.com/questions/34154385/getting-errorinvalid-grant-error-descriptionauthentication-failure-i

2. Second, in your curl command, check if you have left out the security token
 
3. Ensure you have added the Profile of the affected user to the list of App Profiles.

You can do so by going to Manage Apps -> Connected Apps -> (The name of the app)
Then Profiles and "Manage Profiles"

User-added image 
If you followed all of the above and it still does not work, I recommend making a Post request using anonymous apex with the required parameters or using a tool like Postman to see if you are able to recreate the issue and getting additional information in the error message 

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!
This was selected as the best answer
Andrew LambaAndrew Lamba
Relaxing IP restriction worked Thank you!
Shubham Jadhav 70Shubham Jadhav 70
You can refer to blog : 
https://salesforceidiot.blogspot.com/2023/05/how-to-create-connected-app-and-get.html