Salesforce api会话已过期,如何自动重新连接

Salesforce api session expired, how to reconnect automatically

本文关键字:何自动 重新连接 过期 api 会话 Salesforce      更新时间:2023-09-26

我正在使用节点模块jsforce将潜在客户添加到salesforce潜在客户表中。https://www.npmjs.org/package/node-salesforce这是文件。该网站目前正在运行,但每天我都必须在会话到期时重新启动服务器,我都会收到以下消息

{ [INVALID_SESSION_ID: Session expired or invalid] name: 'INVALID_SESSION_ID', errorCode: 'INVALID_SESSION_ID' } undefined

这是启动它的代码部分:

conn.sobject("Lead").create({
  email : req.body.email,
  firstname : req.body.first_name,
  lastname : req.body.last_name,
  title : req.body.job_title,
  company : req.body.company,
  leadsource: 'Clearing Microsite',
  description: req.body.message
}, function(err, ret) {
  if (err || !ret.success) { 
    return console.error(err, ret);
  }
console.log("Created record id : " + ret.id);
  });
});

我猜return console.error(err, ret)是导致会话过期的原因,但发生这种情况后我如何重新连接?

我试过使用conn.logout,然后使用登录功能重新登录,但无法使其正常工作,而且很难测试,因为它似乎只会在一天后过期!

编辑:我想我需要像if (err == {[INVALID...]}) {reconnect}这样的东西,但我不知道该用什么

我想我可能已经解决了它,在if (err || !ret.success) {}中我添加了:

if (err.name == 'INVALID_SESSION_ID') {
  conn.logout(function (err) {
    if (err) { return console.error(err); }
    conn.login(*login details*)
      // then repeated the lead entry
  });
}

直到明天早上会话超时,我才能检查它是否有效,但如果它有效/不有效,我会更新

编辑:不起作用,要在没有注销的情况下尝试。得到这个错误

[Error: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key: <KEY> This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in. ]

第二次编辑:注销就像一个符咒!感谢@supell