Salesforce Authentication Failing

2020-01-27 10:29发布

I am trying to use OAuth authentication to get the Salesforce Authentication Token, so I referred wiki docs, but after getting authorization code, when I make a Post request with 5 required parameters, I'm getting following exception

{"error":"invalid_grant","error_description":"authentication failure"} CODE 400
JSON = {"error":"invalid_grant","error_description":"authentication failure"}

which is I guess a bad request.

PostMethod post = new PostMethod("https://login.salesforce.com/services/oauth2/token");
post.addParameter("code",##############);
post.addParameter("grant_type","authorization_code");
post.addParameter("redirect_uri","#################");  
post.addParameter("client_id",this.client_id);
post.addParameter("client_secret",this.client_secret);
httpclient.executeMethod(post);
String responseBody = post.getResponseBodyAsString();
System.out.println(responseBody+" CODE "+post.getStatusCode());

Kindly reply, if exception known?

10条回答
The star\"
2楼-- · 2020-01-27 11:06

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"

查看更多
姐就是有狂的资本
3楼-- · 2020-01-27 11:08

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
查看更多
手持菜刀,她持情操
4楼-- · 2020-01-27 11:11

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.
查看更多
相关推荐>>
5楼-- · 2020-01-27 11:13

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.

查看更多
登录 后发表回答