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
William PlayfairWilliam Playfair 

JSForce Login Issue

Hello, 

I am using JSForce to try and authenticate with Salesforce and am receiving this error.

[Error: LOGIN_MUST_USE_SECURITY_TOKEN: Invalid username, password, security token; or user locked out. Are you at a new location? When accessing Salesforce--either via a desktop client or the API--from outside of your company’s trusted networks, you must add a security token to your password to log in. To receive a new security token, log in to salesforce.com at http://login.salesforce.com and click Setup | My Personal Information | Reset Security Token.]

I've tried adding the security token several ways: 
 
conn.login('usernamehere', 'passwordhere-securitytokenhere', function(err, res) {
 
conn.login('usernamehere', 'passwordheresecuritytokenhere', function(err, res) {

Does anyone know the proper way to pass the security token with the JSForce module (node, https://jsforce.github.io/)

Thanks!
EnreecoEnreeco
You have to user the second approach (password+token without the dash).
Are you using a sandbox ORG or a production org (in the first case the login URL is test.salesforce.com)?
Are you testing with an internal user or something like a partner / community license user?
--
May the Force.com be with you!
 
Diego NietoDiego Nieto
I had the same problem time ago.

the first thing that you have to know is that if you going to login to a sandbox environment when you instance the conn object you have to do something like this:
let conn = new jsforce.Connection({loginUrl:'https://test.salesforce.com'});
so the connection will be working with sandbox. If you will connect directly to production environment you can leave the connection without parameter:
 
let conn = new jsforce.Connection();
Second thing, credentials. you have to keep in mind the name of your sandbox environment. if you created a sandbox environment with the name "sandboxname", the user that you would use is "someone@anymail.com.sandboxname". if you are connecting to production environment, you can only use the user name (for this example "someone@anymail.com".

Third thing, the password. The password must include the security token next to the password like "PasswordSecuritytoken" without spaces or dashes. so the code for your connection would be like
 
let conn = new jsforce.Connection({loginUrl:'https://test.salesforce.com'}); conn.login('someone@anymail.com.sandboxname','PasswordSecuritytoken');
or in production environment:
 
let conn = new jsforce.Connection();
conn.login('someone@anymail.com','PasswordSecuritytoken');

Hope this helps :)