Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updating README

  • Loading branch information...
commit 814cc84bbd94c29fa126c8dd97a8080de7b4729f 1 parent 313e3ff
@unscene authored
Showing with 45 additions and 132 deletions.
  1. +41 −35 Readme.md
  2. +0 −87 examples/twitter.js
  3. +2 −8 lib/oauth.js
  4. +2 −2 package.json
View
76 Readme.md
@@ -20,33 +20,38 @@ Otherwise:
#Usage
-See the twitter example for a three legged authentication example.
-
-## Creating a client
-Creating a client works exactly to the regular http client, it actually returns an subclassed http client.
-
- client = oauth.createClient(443,'api.twitter.com,true)
-
-## Sending requests
-Sending http requests is the only area that has a couple of differences from the built-in http module:
-
- var headers = {
- some-header: 'some value'
- }
-
- var body = {
- a: 'b'
+See the example.
+
+## Sending request
+Sending requests works very similar to the latest version of node's built-in http library.
+
+ var request = {
+ port: 443,
+ host: 'api.twitter.com',
+ https: true,
+ path: '/1/statuses/update.json',
+ oauth_signature: signer,
+ method: 'POST',
+ body: body
}
- req = client.request('POST', '/request_token', headers, body, null)
+ request = oauth.request(request, function(response) { ... });
req.write(body);
req.end();
-
-We will talk about that last parameter in a minute. With node you can stream the body and the only reason you must provide this up front is for the body to be included in the signature base string calculation, if you are sending a body of type 'application/x-www-form-urlencoded'. The body must be an object (it then gets converted into a properly encoded string). There are a set of default headers included but you can override them by simply providing your own, your headers get merged into the defaults. Be sure to include the same body as you specified in the request, this way you can still stream the body.
-
-You can also specify your own oauth_* parameters in the query string:
-
- req = client.request('POST', '/request_token?oauth_callback=oob', headers, body, null)
+
+The only two difference here between the built-in client and this one, is that you must include 'https: true' and the 'body' must be included in the request options. You must include it so that the base string can be calculated correctly. There are a set of default headers included but you can override them by simply providing your own, your headers get merged into the defaults. Be sure to include the same body as you specified in the request, this way you can still stream the body.
+
+If you must specify your own oauth_* parameters put them in the query string:
+
+ var request = {
+ port: 443,
+ host: 'api.twitter.com',
+ https: true,
+ path: '/something?oauth_callback=oob',
+ oauth_signature: signer,
+ method: 'POST',
+ body: body
+ }
These params get split out and included in the authorization header.
@@ -56,9 +61,14 @@ That last parameter is the only portion that takes some setup. This is the piec
var consumer = oauth.createConsumer('key','secret');
var signer = oauth.createHmac(consumer);
- ..
- client.request('POST', '/request_token', headers, body, signer);
+ var request = {
+ ..
+ oauth_signature: signer,
+ ..
+ }
+
+ client.request(request, function(response) { ... });
If you have an authorized or unauthorized token you can provide that to the createHMAC constructor as well.
You just need to provide the type of signature you want along with the consumer and tokens, requests get automatically signed.
@@ -69,24 +79,20 @@ Consumers and tokens both have a utility method decode() that will take an http
var data = ''
- requestToken.addListener('response', function (response) {
- response.addListener('data', function (chunk) { data+=chunk });
- response.addListener('end', onRequestTokenResponse);
+ oauth.request(request, function(response)){
+ response.on('data', function (chunk) { data+=chunk });
+ response.on('end', function () {
+ token.decode(data);
+ });
});
- function onRequestTokenResponse() {
- token.decode(data);
- ..
- }
-
# Tests
-I am not sure the total code coverage of the tests at this point, but it is quickly getting there.Running the tests requires vows. See [vows](http://vowsjs.org/) to get started.
+See [vows](http://vowsjs.org/) to get started.
Once installed:
vows tests/*
-
## License
(The MIT License)
View
87 examples/twitter.js
@@ -1,87 +0,0 @@
-//Three legged OAuth example
-var oauth = require('../lib/oauth'),
- util = require('util'),
- fs = require('fs');
-
-var consumerKey = '';
-var consumerSecret = '';
-
-//endpoints
-var requestTokenUrl = '/oauth/request_token';
-var accessTokenUrl = '/oauth/access_token';
-var authorizeTokenUrl = '/oauth/authorize';
-
-//oauth setup
-var consumer = oauth.createConsumer(consumerKey,consumerSecret);
-var token = oauth.createToken();
-var signer = oauth.createHmac(consumer);
-
-var request = {
- port: 443,
- host: 'api.twitter.com',
- https: true,
- headers: {
- 'Connection': 'Upgrade',
- 'Upgrade': 'websocket'
- },
- method: 'POST',
- path: requestTokenUrl,
- oauth_signature: signer
-};
-
-var data = '';
-var tokenData = '';
-
-util.p(request);
-
-var requestToken = oauth.request(request, function (response) {
- response.on('data', function (chunk) { data+=chunk });
-
- response.on('end', function() {
-
- token.decode(data);
- util.p(data)
-
- util.print('Visit the following website\n');
- util.print('https://api.twitter.com'+authorizeTokenUrl+'?oauth_token='+token.oauth_token + '\n');
- util.print('Enter verifier>')
-
- stream = process.openStdin();
- stream.addListener('data', function() {
- token.oauth_verifier = chunk.toString('utf8',0,chunk.length-1);
- stream.removeListener('data',arguments.callee);
-
- signer.token = token;
-
- request.path = accessTokenUrl
- var accessToken = client.request(request, function(response) {
- response.addListener('data', function(chunk) { tokenData+=chunk });
- response.addListener('end', function() {
-
- token.decode(tokenData);
-
- var body = { status: 'testing' };
-
- request.path = '/1/statuses/update.json';
- request.body = body;
-
- var r = client.request(request, function(response){
- response.on('data',function(chunk) { data+=chunk })
- response.on('end',function() { util.print(util.inspect(data)); util.print('\n'); });
- });
-
- //The rest of the code is standard node
- var data = '';
-
- request.write(body);
- request.end();
-
- });
- });
-
- accessToken.end();
- });
- });
-});
-
-requestToken.end();
View
10 lib/oauth.js
@@ -87,8 +87,6 @@ exports.fillURL = function(path, host, port, secure) {
}
exports.normalizeURL = function(path) {
-
- debugger;
var p = url.parse(path.replace(/^https/i, 'https').replace(/^http/i, 'http'));
var ret = {
protocol: p.protocol,
@@ -110,7 +108,6 @@ exports.normalizeURL = function(path) {
}
exports.request = function(options, callback) {
- debugger;
if(!options.oauth_signature) {
return http.request(options, callback);
}
@@ -133,7 +130,6 @@ exports.request = function(options, callback) {
options.headers = signRequest(options.method, uri, options.headers,
options.body, options.oauth_signature);
- debugger;
var req;
if (options.https) {
req = https.request(options, callback);
@@ -195,7 +191,7 @@ function signRequest(method, path, headers, body, signature) {
else {
throw new TypeError("Invalid signature type");
}
- debugger;
+
var esc = querystring.escape;
querystring.escape = _encodeURI;
auth = querystring.stringify(exports.normalize(auth), '\",', '=\"');
@@ -299,7 +295,6 @@ exports.createSignature = function(consumer, token) {
};
Signature.prototype.key = function() {
- debugger;
return [
_encodeURI(this.consumer ? this.consumer.oauth_consumer_secret : ''),
_encodeURI(this.token ? this.token.oauth_token_secret : '')
@@ -311,7 +306,7 @@ Signature.prototype.sign = function() {
};
Signature.prototype.baseString = function(method, url, params) {
- debugger;
+
querystring.escape = _encodeURI;
var baseString = [
@@ -320,7 +315,6 @@ Signature.prototype.baseString = function(method, url, params) {
_encodeURI(querystring.stringify(exports.normalize(params)))
].join('&');
- debugger;
return baseString;
};
View
4 package.json
@@ -1,8 +1,8 @@
{
"name": "oauth-client",
- "version": "0.2.0",
+ "version": "0.3.0",
"engines": {
- "node": ">=0.4.8"
+ "node": ">=0.6.9"
},
"author": "Ryan Fairchild",
"contributors": [
Please sign in to comment.
Something went wrong with that request. Please try again.