Permalink
Browse files

Merge branch 'release/0.3.3'

  • Loading branch information...
2 parents 2de5fa7 + 37ba4e9 commit bd096fe5edbe9da792029a01f5f5f6682d586677 @stomita committed Oct 19, 2012
Showing with 54 additions and 20 deletions.
  1. +38 −12 README.md
  2. +14 −6 lib/connection.js
  3. +2 −2 package.json
View
@@ -5,7 +5,8 @@
## Abstract
Node-salesforce, which is designed to be a wrapper of Salesforce REST API in Node.js, enables Salesforce application development in event-driven style.
-It only capsulates the access of REST API end point, so it works both on OAuth2 access token and SOAP API sessionId.
+It capsulates the access to REST API end point in asynchronous JavaScript function call.
+You can use both OAuth2 access token and SOAP login sessionId for API authentication.
## Install
@@ -41,7 +42,25 @@ var sf = require('node-salesforce');
var conn = new sf.Connection({
instanceUrl : 'https://na1.salesforce.com',
accessToken : '<your Salesforrce OAuth2 access token is here>'
-// refreshToken : '<your Salesforce OAuth2 refresh token is here>'
+});
+```
+
+### Using OAuth2 Access Token with Refresh Token (automatically refresh access token when expired)
+
+```javascript
+var sf = require('node-salesforce');
+var conn = new sf.Connection({
+ oauth2 : {
+ clientId : '<your Salesforce OAuth2 client ID is here>',
+ clientSecret : '<your Salesforce OAuth2 client secret is here>',
+ redirectUri : '<your Salesforce OAuth2 redirect URI is here>'
+ },
+ instanceUrl : 'https://na1.salesforce.com',
+ accessToken : '<your Salesforrce OAuth2 access token is here>',
+ refreshToken : '<your Salesforce OAuth2 refresh token is here>'
+});
+conn.on("refresh", function(accessToken, res) {
+ // you can store renewed access token in your storage for next request
});
```
@@ -67,10 +86,13 @@ conn.login(username, password, function(err) {
```javascript
var sf = require('node-salesforce');
var conn = new sf.Connection({
-// loginUrl : 'https://login.salesforce.com'
- clientId : '<your Salesforce OAuth2 client ID is here>',
- clientSecret : '<your Salesforce OAuth2 client secret is here>',
- redirectUri : '<callback URI is here>'
+ // you can change loginUrl to connect to sandbox or prerelease env.
+ // loginUrl : 'https://test.salesforce.com'
+ oauth2 : {
+ clientId : '<your Salesforce OAuth2 client ID is here>',
+ clientSecret : '<your Salesforce OAuth2 client secret is here>',
+ redirectUri : '<callback URI is here>'
+ }
});
conn.login(username, password, function(err) {
if (!err) {
@@ -93,9 +115,11 @@ var sf = require('node-salesforce');
// get authz url and redirect to it.
app.get('/oauth2/auth', function(req, res) {
var conn = new sf.Connection({
- clientId : '<your Salesforce OAuth2 client ID is here>',
- clientSecret : '<your Salesforce OAuth2 client secret is here>',
- redirectUri : '<callback URI is here>'
+ oauth2 : {
+ clientId : '<your Salesforce OAuth2 client ID is here>',
+ clientSecret : '<your Salesforce OAuth2 client secret is here>',
+ redirectUri : '<callback URI is here>'
+ }
});
res.redirect(conn.oauth2.getAuthorizationUrl({ scope : 'api id web' }));
});
@@ -107,9 +131,11 @@ app.get('/oauth2/auth', function(req, res) {
// pass received authz code and get access token
app.get('/oauth2/callback', function(req, res) {
var conn = new sf.Connection({
- clientId : '<your Salesforce OAuth2 client ID is here>',
- clientSecret : '<your Salesforce OAuth2 client secret is here>',
- redirectUri : '<callback URI is here>'
+ oauth2 : {
+ clientId : '<your Salesforce OAuth2 client ID is here>',
+ clientSecret : '<your Salesforce OAuth2 client secret is here>',
+ redirectUri : '<callback URI is here>'
+ }
});
var code = req.param('code');
conn.authorize(code, function(err) {
View
@@ -43,14 +43,22 @@ Connection.prototype.initialize = function(options) {
base : [ this.instanceUrl, "cometd", this.version ].join('/')
}
};
- if (options.clientId) {
- this.oauth2 = new OAuth2({
- authzServiceUrl : this.loginUrl + "/services/oauth2/authorize",
- tokenServiceUrl : this.loginUrl + "/services/oauth2/token",
+ var oauth2 = options.oauth2;
+ if (!oauth2 && options.clientId) { // if oauth2 client config is written in flat (for compatibility)
+ oauth2 = {
clientId : options.clientId,
clientSecret : options.clientSecret,
redirectUri : options.redirectUri
- });
+ };
+ }
+ if (oauth2) {
+ if (!(oauth2 instanceof OAuth2)) {
+ oauth2 = new OAuth2(_.extend({
+ authzServiceUrl : this.loginUrl + "/services/oauth2/authorize",
+ tokenServiceUrl : this.loginUrl + "/services/oauth2/token"
+ }, oauth2));
+ }
+ this.oauth2 = oauth2;
}
this.accessToken = options.sessionId || options.accessToken || this.accessToken;
this.refreshToken = options.refreshToken || this.refreshToken;
@@ -80,7 +88,7 @@ Connection.prototype._request = function(params, callback, noContentResponse) {
}
params.headers = params.headers || {};
if (this.accessToken) {
- params.headers.Authorization = "OAuth " + this.accessToken;
+ params.headers.Authorization = "Bearer " + this.accessToken;
}
self.emit('request', params.method, params.url, params);
request(params, function(err, response) {
View
@@ -1,10 +1,10 @@
{
"author": "Shinichi Tomita <shinichi.tomita@gmail.com>",
"name": "node-salesforce",
- "description": "Connecting Salesforce via RESTful API",
+ "description": "Salesforce API Connection Library for Node.js Applications",
"keywords": [ "salesforce", "salesforce.com", "sfdc", "force.com", "database.com" ],
"homepage": "http://github.com/stomita/node-salesforce",
- "version": "0.3.2",
+ "version": "0.3.3",
"repository": {
"type": "git",
"url": "git://github.com/stomita/node-salesforce.git"

0 comments on commit bd096fe

Please sign in to comment.