2017-01-04 81 views
1
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,我無法得到它。通過節點連接到salesforce後獲取會話ID

回答

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); 
    }); 
    }); 
});