[SalesForce] SOAP API – not working only on Sandboxes

SOAP API is not able to authenticate using username/password+security only on sandboxes.
I tried to authenticate the username/pwd+Security token using mavens mate and they seem to be correct.

Could this be due to TSL disablement? If so, what is the fix for this? I see fixes around using appropriate Java versions to support this change, but what should I do from Apex to allow SOAP logins?I am getting '

Web service callout failed: WebService returned a SOAP Fault:
INVALID_LOGIN: Invalid username, password, security token; or user
locked out. faultcode=INVALID_LOGIN faultactor=' exception which does
not give any hint about the TLS disablement.

partnerSoapSforceCom.Soap con = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult loginResult = con.login('username', 'passwordtoken');

Please note that it is happening only for sandboxes and not production.
Any ideas, appreciated.

Best Answer

From the example code you provided, it looks like you are accessing the SOAP Partner API from Apex generated via wsdl2apex.

You will need to adjust the initial endpoint of the login call for a sandbox. Sandbox logins need to go via test.salesforce.com. All others (except preproduction) start with login.salesforce.com.

After you complete the login, use the serverUrl from the login result to set the endpoint.

partnerSoapSforceCom.Soap partner = new partnerSoapSforceCom.Soap();

// Sandbox
partner.endpoint_x = 'https://test.salesforce.com/services/Soap/u/39.0';
// Production/Developer 
//partner.endpoint_x = 'https://login.salesforce.com/services/Soap/u/39.0';

partnerSoapSforceCom.LoginResult lr = partner.login('user@example.com', 'Password' + 'SecurityToken');
partnerSoapSforceCom.SessionHeader_element header = new partnerSoapSforceCom.SessionHeader_element();
header.sessionId=lr.sessionId;
partner.SessionHeader=header;
partner.endpoint_x = lr.serverUrl;

partnerSoapSforceCom.QueryResult qr = partner.Query('Select Id, Name from Account limit 1');
Related Topic