在Chrome扩展中使用Node.js和Express.js

Using Node.js and Express.js in Chrome-Extension

本文关键字:js Node Express Chrome 扩展      更新时间:2023-09-26

我正试图使用JSforce.js提供的oauth2流构建一个连接到salesforce的chrome扩展,但我在网上找到的所有示例都包含Node.jsExpress.js

编辑:

我从这里得到了这个例子:链接

这是使用express.js框架运行的Oauth2代码:

 var jsforce = require('jsforce');
//
// OAuth2 client information can be shared with multiple connections.
//
var oauth2 = new sf.OAuth2({
  // you can change loginUrl to connect to sandbox or prerelease env.
  // loginUrl : 'https://test.salesforce.com',
  clientId : '<your Salesforce OAuth2 client ID is here>',
  clientSecret : '<your Salesforce OAuth2 client secret is here>',
  redirectUri : '<callback URI is here>'
});
//
// Get authz url and redirect to it.
//
app.get('/oauth2/auth', function(req, res) {
  res.redirect(oauth2.getAuthorizationUrl({ scope : 'api id web' }));
});
// Pass received authz code and get access token
//
app.get('/oauth2/callback', function(req, res) {
  var conn = new sf.Connection({ oauth2 : oauth2 });
  var code = req.param('code');
  conn.authorize(code, function(err, userInfo) {
    if (err) { return console.error(err); }
    // Now you can get the access token, refresh token, and instance URL information.
    // Save them to establish connection next time.
    console.log(conn.accessToken);
    console.log(conn.refreshToken);
    console.log(conn.instanceUrl);
    console.log("User ID: " + userInfo.id);
    console.log("Org ID: " + userInfo.organizationId);
    // ...
  });
});

require(),app.get(),sf。Oauth2()是Node.js和Express.js提供的对象/函数,我不能使用

Node.js不应该在浏览器或chrome扩展中运行,您应该按照以下说明使用web浏览器设置,并将jsforce.js添加到扩展的页面中。