Salesforce Authentication Failing

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:24:51
Calvin Froedge

For anyone who is as stuck and frustrated as I was, I've left a detailed blog post on the entire process (with pictures and ranty commentary!). Click the link if you want that:

http://www.calvinfroedge.com/salesforce-how-to-generate-api-credentials/

Here is a text only answer:

Step 1:

Create an account. You can create a (free) developer account at developer.salesforce.com


Step 2:

Ignore all the landing pages and getting started crap. It's an endless marketing loop.


Step 3:

Click the "Setup" link


Step 4:

In the lefthand toolbar, under "Create", click "Apps"


Step 5:

Under "Connected Apps" click "New"


Step 6:

Fill out the form. Important fields are the ones marked as required, and the oauth section. Note that you can leave any url for your callback (I used localhost).


Step 7:

Be advised that Salesforce has crappy availability.


Step 8:

Press continue. You finally have your client_id key (labelled 'Consumer Key') and client_secret (labelled 'Consumer Secret').


Step 9:

But wait! You're not done yet; select 'Manage' then 'Edit Policies'

  1. Make sure IP relaxation is set to Relax IP restrictions,

  2. and make sure that Permitted Users is set to "All users may self-authorize.",

  3. and also make sure the your Security > Network Access > Trusted IP Ranges has been set

If you're concerned about disabling security, don't be for now, you just want to get this working for now so you can make API calls. Tighten permissions once you have everything working, one at a time, so you can figure out what setting is giving you authentication errors.


Step 10:

Celebrate! This curl call should succeed:

on production:

curl -v https://login.salesforce.com/services/oauth2/token \
  -d "grant_type=password" \
  -d "client_id=YOUR_CLIENT_ID_FROM_STEP_8" \
  -d "client_secret=YOUR_CLIENT_SECRET_FROM_STEP_8" \
  -d "username=user@wherever.com" -d "password=foo@bar.com"

on sandbox or test:

curl -v https://test.salesforce.com/services/oauth2/token \
  -d "grant_type=password" \
  -d "client_id=YOUR_CLIENT_ID_FROM_STEP_8" \
  -d "client_secret=YOUR_CLIENT_SECRET_FROM_STEP_8" \
  -d "username=user@wherever.com" -d "password=foo@bar.com"

Notes:

  • You shouldn't be doing password authorization if you're building a multi-tenant app, where users need to authorize their own application. Use the Oauth2 workflow for that.

  • You may need to pass in your security token appended to your password.

dotNetkow

We had this issue as well.

Check your Connected App settings - under Selected OAuth Scopes, you may need to adjust the selected permissions. Our app primarily uses Chatter, so we had to add both:

  • Access and manage your Chatter feed (chatter_api)
  • Perform requests on your behalf at any time (refresh_token).

Again, your mileage may vary but try different combinations of permissions based on what your Application does/needs.

Additionally, the actual invalid_grant error seems to occur due to IP restrictions. Ensure that the server's IP address that is running the OAuth authentication code is allowed. I found that if the SFDC environment has IP restriction setting Enforce IP restrictions set (Setup -> Administer -> Manage Apps -> Connected Apps), then each User Profile must have the allowed IP addresses as well.

TL:DR

For OAuth 2 tokens if you login...

Story:

  1. I was following Salesforce "Set Up OAuth 2.0"
  2. Credentials were correct (many character by character checks)
  3. When I'd call curl https://login.salesforce.com/services/oauth2/token -d "...credentials..." it still failed with:

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

Solution:

Realized there are different OAuth environments when reading Digging Deeper into OAuth 2.0 in Salesforce specifically (emphasis added):

OAuth 2.0 Authentication Endpoints

OAuth endpoints are the URLs that you use to make OAuth authentication requests to Salesforce. When your application makes an authentication request, make sure you’re using the correct Salesforce OAuth endpoint. The primary endpoints are:

Instead of login.salesforce.com, customers can also use the My Domain, community, or test.salesforce.com (sandbox) domains in these endpoints.

Fix

Because I logged into my environment via test.salesforce.com switching to curl https://test.salesforce.com/services/oauth2/token -d "...credentials..." resulted in a "Congrats! (>^_^)> Give OAuth token response"

Amir M

Salesforce is requiring an upgrade to TLS 1.1 or higher by July 22, 2017 in order to align with industry best practices for security and data integrity: from help.salesforce.com.

try to add this code:

System.Net.ServicePointManager.SecurityProtocol = 
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

Another option is to edit your registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

Check this link for more detailed answers: Default SecurityProtocol in .NET 4.5

To whitelist an IP address range follow these steps:

  1. Click Setup in the top-right
  2. Select Administer > Security Controls > Network Access from the left navigation
  3. Click New
  4. Add your ip address range
  5. Click Save

Replace your Salesforce password with combination of the password and the security token. For example, if your password is "MyPassword" and your security token is "XXXXXX", you would need to enter "MyPasswordXXXXXX" in the password field.

If you do not have the security token you can reset it as below.

  • Go to Your Name --> My Settings --> Personal --> Reset My Security Token.

You can call your APEX controller using Postman if you enter the Consumer Key and Consumer Secret in the Access Token settings - you don't need the Security Token for this.

Set up the Authorization like this screenshot...

Postman OAuth 2.0

And enter your credentials on the window after hitting the Get New Access Token button...

Get Access Token

Then hit the Request Token button to generate a token, then hit the Use Token button and it will populate the Access Token field on the Authorization tab where you hit the Get New Access Token button.

That Guy

I was banging my head against the desk trying to get this to work. Turns out my issue was copying and pasting, which messed up the " character. I went and manually typed " pasted that into the command line and then it worked.

I had the same error with all keys set correct and spent a lot of time trying to figure out why I cannot connect.

Finally I've found that in Setup -> Manage Connected Apps -> Click "MyAppName" -> Click "Edit Policies".

In the 'Permitted Users' field value "All users may self-authorize" should be set.

I tried many solutions above which did not work for me. However the trick that actually worked for me was to stop using curl and to use postman application to make the request instead.

By replicating the request in postman, with a POST request and the following params

  1. grant_type
  2. client_id
  3. client_secret
  4. username
  5. password

This solved the issue for me.

Just posting it here in case there are others who have tried all the possible solutions with no avail (like I did).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!