JSforce-metadata-tools 使用refresh Token登入Salesforce

jsforce-metadata-tools

jsforce-metadata-tools 目前不支持传入refresh token作为参数,有时候需要输入账号和密码,而输入账号和密码,往往因为服务器IP地址问题,会出现如下error

1
2
Error: LOGIN_MUST_USE_SECURITY_TOKEN: Invalid username, password, security token; or user locked out. Are you at a new location? When accessing Salesforce--either via a desktop client or the API--from outside of your company’s trusted netwo
rks, you must add a security token to your password to log in. To get your new security token, log in to Salesforce. From your personal settings, enter Reset My Security Token in the Quick Find box, then select Reset My Security Token.

而如果我们通过refresh token进行登入Salesforce,是不需要输入账号和密码的。

作者源码在这里:

https://github.com/jsforce/jsforce-metadata-tools

首先需要在cli.js里面加入如下三个option

1
2
3
.option('-i, --client_id [client_id]', 'Salesforce client_id')
.option('-s, --client_secret [client_secret]', 'Salesforce client_secret')
.option('-r, --refresh_token [refresh_token]', 'Salesforce refresh_token')

在这里,我们输入三个参数client_id,client_secret,refresh_token。
同时,在readOptions的function下,将string变成’username,password,connection,loginUrl,pollInterval,pollTimeout,refresh_token,client_id,client_secret’,也就是说会读取client id,client secret,refresh token。

最后,在connect.js下面修改成动态可以以refresh token进行登入Salesforce。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';

var jsforce = require('jsforce');
var Promise = jsforce.Promise;
var request = require('request');

var CONNECTION_CONFIG_PROPS = 'loginUrl,accessToken,instanceUrl,refreshToken,clientId,clientSecret,redirectUri,logLevel,version'.split(
','
);

/**
*
*/
function connect(options) {
var conn;
return Promise.resolve()
.then(function() {
if (options.connection) {
console.log(' ** connection ** ');
conn = jsforce.registry.getConnection(options.connection);
if (!conn) {
throw new Error('No connection named "' + options.connection + '" in registry');
}
return null;
} else if (options.refresh_token) {
return LoginUsingRT(
options.refresh_token,
options.client_id,
options.client_secret,
options.loginUrl
);
} else if (options.username && options.password) {
var config = {};
CONNECTION_CONFIG_PROPS.forEach(function(prop) {
if (options[prop]) {
config[prop] = options[prop];
}
});
conn = new jsforce.Connection(config);
return conn.login(options.username, options.password);
} else {
return conn.identity().catch(function(err) {
throw new Error(
'Credential to salesforce server is not found in options.\n' +
'Specify "username" and "password" in options, or give any other credentials.'
);
});
}
})
.then(function(result) {
if (result != null) {
if (options.refresh_token) {
conn = new jsforce.Connection({
loginUrl: options.loginUrl,
instanceUrl: result.instance_url,
accessToken: result.access_token,
version: '45.0'
});

return conn;
}
}
})
.then(function() {
if (options.logger) {
var logger = options.logger;
return conn.identity().then(function(identity) {
logger.log('Logged in as: ' + identity.username);
return conn;
});
}
return conn;
});
}

function LoginUsingRT(refresh_token, client_id, client_secret,loginUrl) {
var SFoptions = {
method: 'POST',
url: loginUrl+'/services/oauth2/token',
qs: {
grant_type: 'refresh_token',
refresh_token: refresh_token,
client_id: client_id,
client_secret: client_secret
},
headers: {
'cache-control': 'no-cache'
}
};

return new Promise(function(resolve, reject) {
request(SFoptions, function(error, response, body) {
if (error) {
reject(error);
} else {
resolve(JSON.parse(body));
}
});
});
}

/**
*
*/
module.exports = connect;

例子如下:

1
2
3
4
5
6
cd bin

node jsforce-retrieve -l https://test.salesforce.com -D ./SCMP-QA1
-r 你的refresh Token
-i 你的client id
-s 你的client_secret

很多时候我们使用JSforce多于JSforce metadata tools,下面的例子展示了如何基于JSForce使用refresh token登入Salesforce。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// node jsforce.js
var request = require("request");
var jsforce = require('jsforce');
var conn;

main();

function main() {
var access_token, instance_url;
initializeSF().then(function(result) {//console.log(result);
access_token = result.access_token;
instance_url = result.instance_url;
return GetConnection(access_token, instance_url);

}).then(function(result) {

return conn.identity();
}).then(function(result) {
console.log('Logged in as: ' + result.username);

}).catch(function(err) {
console.log(err);
});
}


function initializeSF() {
var SFoptions = {
method: 'POST',
url: 'https://test.salesforce.com/services/oauth2/token',
qs: {
grant_type: 'refresh_token',
refresh_token: '你的refresh_token',
client_id: '你的client_id',
client_secret: '你的client_secret'
},
headers: {
'cache-control': 'no-cache'
}
};

return new Promise(function(resolve, reject) {
request(SFoptions, function(error, response, body) {
if (error) {
reject(error);
}
else {
resolve(JSON.parse(body));
}
})
})
}

function GetConnection(access_token, instance_url) {
conn = new jsforce.Connection({
loginUrl: 'https://test.salesforce.com',
instanceUrl: instance_url,
accessToken: access_token,
version: '44.0'
});

return conn;
}