通过节点连接到salesforce后获取会话ID

时间:2017-01-04 13:53:22

标签: node.js salesforce

const sf = require('node-salesforce');
const conn = new sf.Connection({
  loginUrl: config.get('salesforce').loginUrl
});  
  conn.login(config.get('salesforce').login, `${config.get('salesforce').password}${config.get('salesforce').token}`, function(err, res) {
      conn.query(`SELECT Id, Name FROM sessions WHERE id = '${res.id}'`, function(err, res) {
        if (err) { return console.error(err); }
        console.log(res);
      });
    });

我已成功连接到salesforce,可以获得查询。 但是我也需要sessionId而且我无法得到它。

1 个答案:

答案 0 :(得分:0)

我建议使用jsforce而不是node-salesforce。

在这里查看我的一个博客,了解jsforce和节点:http://nodexperts.com/blog/salesforce-oauth-using-nodejs/

app.get('/oauth2/auth', function(req, res) {
  const oauth2 = new jsforce.OAuth2({
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET_ID,
    redirectUri: `${req.protocol}://${req.get('host')}/${process.env.REDIRECT_URI}`
  });
  res.redirect(oauth2.getAuthorizationUrl({}));
});


app.get('/getAccessToken', function(req,res) {
  const oauth2 = new jsforce.OAuth2({
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET_ID,
    redirectUri: `${req.protocol}://${req.get('host')}/${process.env.REDIRECT_URI}`
  });
  const conn = new jsforce.Connection({ oauth2 : oauth2 });

  conn.authorize(req.query.code, function(err, userInfo) {
    if (err) {
      return console.error(err);
    }
    const conn2 = new jsforce.Connection({
      instanceUrl : conn.instanceUrl,
      accessToken : conn.accessToken
    });
    conn2.identity(function(err, res) {
      if (err) { return console.error(err); }
      console.log("user ID: " + res.user_id);
      console.log("organization ID: " + res.organization_id);
      console.log("username: " + res.username);
      console.log("display name: " + res.display_name);
    });
  });
});