Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'release/0.3.3'

  • Loading branch information...
commit bd096fe5edbe9da792029a01f5f5f6682d586677 2 parents 2de5fa7 + 37ba4e9
Shinichi Tomita authored

Showing 3 changed files with 54 additions and 20 deletions. Show diff stats Hide diff stats

  1. +38 12 README.md
  2. +14 6 lib/connection.js
  3. +2 2 package.json
50 README.md
Source Rendered
@@ -5,7 +5,8 @@
5 5 ## Abstract
6 6
7 7 Node-salesforce, which is designed to be a wrapper of Salesforce REST API in Node.js, enables Salesforce application development in event-driven style.
8   -It only capsulates the access of REST API end point, so it works both on OAuth2 access token and SOAP API sessionId.
  8 +It capsulates the access to REST API end point in asynchronous JavaScript function call.
  9 +You can use both OAuth2 access token and SOAP login sessionId for API authentication.
9 10
10 11 ## Install
11 12
@@ -41,7 +42,25 @@ var sf = require('node-salesforce');
41 42 var conn = new sf.Connection({
42 43 instanceUrl : 'https://na1.salesforce.com',
43 44 accessToken : '<your Salesforrce OAuth2 access token is here>'
44   -// refreshToken : '<your Salesforce OAuth2 refresh token is here>'
  45 +});
  46 +```
  47 +
  48 +### Using OAuth2 Access Token with Refresh Token (automatically refresh access token when expired)
  49 +
  50 +```javascript
  51 +var sf = require('node-salesforce');
  52 +var conn = new sf.Connection({
  53 + oauth2 : {
  54 + clientId : '<your Salesforce OAuth2 client ID is here>',
  55 + clientSecret : '<your Salesforce OAuth2 client secret is here>',
  56 + redirectUri : '<your Salesforce OAuth2 redirect URI is here>'
  57 + },
  58 + instanceUrl : 'https://na1.salesforce.com',
  59 + accessToken : '<your Salesforrce OAuth2 access token is here>',
  60 + refreshToken : '<your Salesforce OAuth2 refresh token is here>'
  61 +});
  62 +conn.on("refresh", function(accessToken, res) {
  63 + // you can store renewed access token in your storage for next request
45 64 });
46 65 ```
47 66
@@ -67,10 +86,13 @@ conn.login(username, password, function(err) {
67 86 ```javascript
68 87 var sf = require('node-salesforce');
69 88 var conn = new sf.Connection({
70   -// loginUrl : 'https://login.salesforce.com'
71   - clientId : '<your Salesforce OAuth2 client ID is here>',
72   - clientSecret : '<your Salesforce OAuth2 client secret is here>',
73   - redirectUri : '<callback URI is here>'
  89 + // you can change loginUrl to connect to sandbox or prerelease env.
  90 + // loginUrl : 'https://test.salesforce.com'
  91 + oauth2 : {
  92 + clientId : '<your Salesforce OAuth2 client ID is here>',
  93 + clientSecret : '<your Salesforce OAuth2 client secret is here>',
  94 + redirectUri : '<callback URI is here>'
  95 + }
74 96 });
75 97 conn.login(username, password, function(err) {
76 98 if (!err) {
@@ -93,9 +115,11 @@ var sf = require('node-salesforce');
93 115 // get authz url and redirect to it.
94 116 app.get('/oauth2/auth', function(req, res) {
95 117 var conn = new sf.Connection({
96   - clientId : '<your Salesforce OAuth2 client ID is here>',
97   - clientSecret : '<your Salesforce OAuth2 client secret is here>',
98   - redirectUri : '<callback URI is here>'
  118 + oauth2 : {
  119 + clientId : '<your Salesforce OAuth2 client ID is here>',
  120 + clientSecret : '<your Salesforce OAuth2 client secret is here>',
  121 + redirectUri : '<callback URI is here>'
  122 + }
99 123 });
100 124 res.redirect(conn.oauth2.getAuthorizationUrl({ scope : 'api id web' }));
101 125 });
@@ -107,9 +131,11 @@ app.get('/oauth2/auth', function(req, res) {
107 131 // pass received authz code and get access token
108 132 app.get('/oauth2/callback', function(req, res) {
109 133 var conn = new sf.Connection({
110   - clientId : '<your Salesforce OAuth2 client ID is here>',
111   - clientSecret : '<your Salesforce OAuth2 client secret is here>',
112   - redirectUri : '<callback URI is here>'
  134 + oauth2 : {
  135 + clientId : '<your Salesforce OAuth2 client ID is here>',
  136 + clientSecret : '<your Salesforce OAuth2 client secret is here>',
  137 + redirectUri : '<callback URI is here>'
  138 + }
113 139 });
114 140 var code = req.param('code');
115 141 conn.authorize(code, function(err) {
20 lib/connection.js
@@ -43,14 +43,22 @@ Connection.prototype.initialize = function(options) {
43 43 base : [ this.instanceUrl, "cometd", this.version ].join('/')
44 44 }
45 45 };
46   - if (options.clientId) {
47   - this.oauth2 = new OAuth2({
48   - authzServiceUrl : this.loginUrl + "/services/oauth2/authorize",
49   - tokenServiceUrl : this.loginUrl + "/services/oauth2/token",
  46 + var oauth2 = options.oauth2;
  47 + if (!oauth2 && options.clientId) { // if oauth2 client config is written in flat (for compatibility)
  48 + oauth2 = {
50 49 clientId : options.clientId,
51 50 clientSecret : options.clientSecret,
52 51 redirectUri : options.redirectUri
53   - });
  52 + };
  53 + }
  54 + if (oauth2) {
  55 + if (!(oauth2 instanceof OAuth2)) {
  56 + oauth2 = new OAuth2(_.extend({
  57 + authzServiceUrl : this.loginUrl + "/services/oauth2/authorize",
  58 + tokenServiceUrl : this.loginUrl + "/services/oauth2/token"
  59 + }, oauth2));
  60 + }
  61 + this.oauth2 = oauth2;
54 62 }
55 63 this.accessToken = options.sessionId || options.accessToken || this.accessToken;
56 64 this.refreshToken = options.refreshToken || this.refreshToken;
@@ -80,7 +88,7 @@ Connection.prototype._request = function(params, callback, noContentResponse) {
80 88 }
81 89 params.headers = params.headers || {};
82 90 if (this.accessToken) {
83   - params.headers.Authorization = "OAuth " + this.accessToken;
  91 + params.headers.Authorization = "Bearer " + this.accessToken;
84 92 }
85 93 self.emit('request', params.method, params.url, params);
86 94 request(params, function(err, response) {
4 package.json
... ... @@ -1,10 +1,10 @@
1 1 {
2 2 "author": "Shinichi Tomita <shinichi.tomita@gmail.com>",
3 3 "name": "node-salesforce",
4   - "description": "Connecting Salesforce via RESTful API",
  4 + "description": "Salesforce API Connection Library for Node.js Applications",
5 5 "keywords": [ "salesforce", "salesforce.com", "sfdc", "force.com", "database.com" ],
6 6 "homepage": "http://github.com/stomita/node-salesforce",
7   - "version": "0.3.2",
  7 + "version": "0.3.3",
8 8 "repository": {
9 9 "type": "git",
10 10 "url": "git://github.com/stomita/node-salesforce.git"

0 comments on commit bd096fe

Please sign in to comment.
Something went wrong with that request. Please try again.