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
mannsandeepmannsandeep 

oAuth2 token - invalid server error code 500

trying to get access authorization token thru java code but salesforce either return "internal server error - 500". 
Same URL and parameters work fine from postman 

public class RestWork
{
    private static final String CONSUMER_KEY = "3MVG9PbXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXzCH";
    private static final String CONSUMER_SECRET = "11111111111111111111";
       
    public static void main(String[] args) throws IOException
    {
              HttpClient client =  new DefaultHttpClient();;
              String loginURL = "https://cs51.salesforce.com/services/oauth2/token";    
        
        HttpPost httpPost = new HttpPost(loginURL);
       
     params.add(new BasicNameValuePair("client_id",  URLEncoder.encode(CONSUMER_KEY,"UTF-8")));
     params.add(new BasicNameValuePair("client_secret",  URLEncoder.encode(CONSUMER_SECRET,"UTF-8")));
     params.add(new BasicNameValuePair("grant_type", URLEncoder.encode("password","UTF-8")));
     params.add(new BasicNameValuePair("username", "nCCCCC@XXXXXX.com"));
     params.add(new BasicNameValuePair("password",  "PPPPPPPPPPP!23dddddddddddddddddddddddddd"));
  
    httpPost.setEntity(new UrlEncodedFormEntity(params));
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse response = client.execute(httpPost);
       String body = EntityUtils.toString(response.getEntity());
         System.out.println(body);
      }
}
SandhyaSandhya (Salesforce Developers) 
Hi,


I suspect the issue is around the security token and the Trusted IP Ranges in the Network Access Security Controls.

Something to check, look for failed logins under Administration Setup > Manage Users > Login History. If you see the Status message "Failed: API security token required" then the requesting Salesforce IP address isn't trusted.
If the IP address that the login request is coming from isn't trusted Salesforce will reject it unless you append the security token on the end of the password.
So your options are:

Modify the Visualforce page to prompt the user to append their security token. You can't get this in Apex code.It is emailed to the user using the Reset my Security Token link.

Add the requesting Salesforce servers IP address to the Trusted IP addresses under Administration Setup > Securtiy Controls > Network Access: Trusted IP Ranges. You could also check Salesforce IP Address to whitelist

https://help.salesforce.com/HTViewSolution?id=000003652
 

Please refer below links for similar discussion.

https://developer.salesforce.com/forums/?id=906F00000009CYuIAM

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm

http://salesforce.stackexchange.com/questions/53969/salesforce-api-login-status-server-error-statuscode-500
Hope this helps you!


Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya