1

I couldn't find how to login to salesforce organisation like mavens mate does when user creates new project. I did't find any examples. Can somebody show some example? I mean login using REST API call, without Auth I think.

I will add some code:

var http = require('http');
var querystring = require('querystring');

var postData = querystring.stringify({
username: 'someusername',
password: 'somepassword',
grant_type: 'password'
});

var options = {
hostname: 'login.salesforce.com',
port: 80,
method: 'POST',
headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
}
};

var req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
    console.log('No more data in response.')
})
});

req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();

So, I did't get what I expected like in Documentation for REST API. What is wrong with this code? The response of the code above: STATUS: 302 HEADERS: {"date":"Sun, 03 Apr 2016 04:52:11 GMT","set-cookie":["BrowserId=UxnWs1WlSPeTEXoWQRjVbA;Path=/;Domain=.salesforce.com;Expires=Thu, 02-Jun-2016 04:52:11 GMT"],"expires":"Thu, 01 Jan 1970 00:00:00 GMT","strict-transport-security":"max-age=10886400; includeSubDomains; preload","content-type":"text/html;charset=UTF-8","pragma":"no-cache","cache-control":"no-cache, no-store","location":"https://login.salesforce.com/","content-length":"0","connection":"close"} No more data in response.

0

Using Username and Password there are couple of ways I can think of

1.Using SOAP API to Login

You can use the Partner WSDL or Enterprise WSDL and that has a method to parse and use login() call

2.Using Oauth Username and Password Flow

Both of these flows just use username and Password the User enters to retrieve the session Id .

Update :

I see you are using a Node for this .Since you use Node.js there are lot of libraries doing authentication for you

Check below npm package

You can use nforce as well to this

You will create a simple connected app

var nforce = require('nforce');

 var org = nforce.createConnection({
 clientId: 'SOME_OAUTH_CLIENT_ID',
 clientSecret: 'SOME_OAUTH_CLIENT_SECRET',
 redirectUri: 'http://localhost:3000/oauth/_callback',
 apiVersion: 'v27.0',  // optional, defaults to current salesforce API version
  environment: 'production',  // optional, salesforce 'sandbox' or 'production', production default
  mode: 'multi' // optional, 'single' or 'multi' user mode, multi default
});

Then use below

 // multi user mode
 var oauth;
 org.authenticate({ username: 'my_test@gmail.com', password: 'mypassword'},    function(err, resp){
 // store the oauth object for this user
  if(!err) oauth = resp;
  });
12
  • I tried, but filed. I edited a little bit my question.
    – Iaroslav
    Apr 3 '16 at 5:08
  • Updated the answer ,.Check if that helps Apr 3 '16 at 5:16
  • Yes. But when I use any force.com ide, I don't use any clientId or clientSecret. Right? I use only username and password. So, whate I am trying to do is to mimic login logic of any force.com ide.
    – Iaroslav
    Apr 3 '16 at 5:23
  • You use SOAP login there.Force.com IDE uses WSDL file to generate a login method .While mordern way is to use connected app and if you need to use username and password use username-password flow Apr 3 '16 at 5:26
  • Mavens mate as I think use Node.js. I suppose they use rest api. In either way I should be able to use this too. In some way they have implemented this too. Right?
    – Iaroslav
    Apr 3 '16 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.