Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit af9bb45d4a87a422a3bc8b75feee054c9ea08221 @ddollar committed Oct 23, 2010
Showing with 150 additions and 0 deletions.
  1. +23 −0 README.md
  2. +9 −0 index.js
  3. +98 −0 lib/client.js
  4. +20 −0 package.json
23 README.md
@@ -0,0 +1,23 @@
+# twitter-connect
+
+Twitter client meant to be used with connect apps.
+
+## Installation
+
+ $ npm install twitter-connect
+
+
+## Usage
+
+ var twitter = require('twitter-connect').createClient({
+ consumerKey: 'consumer_key_here',
+ consumerSecret: 'consumer_secret_here'
+ });
+
+ app.get('/test', function(request, response) {
+ twitter.authorize(request, response, function(error, api) {
+ api.get('/account/verify_credentials.json', function(error, data) {
+ response.send('DATA: ' + sys.inspect(data))
+ });
+ });
+ });
9 index.js
@@ -0,0 +1,9 @@
+var exports = module.exports;
+
+exports.version = JSON.parse(require('fs').readFileSync(__dirname + '/package.json')).version;
+
+var Client = require('./lib/client');
+
+exports.createClient = function(consumerKey, consumerSecret) {
+ return new Client(consumerKey, consumerSecret);
+}
98 lib/client.js
@@ -0,0 +1,98 @@
+var OAuth = require('oauth').OAuth;
+var URL = require("url");
+
+module.exports = function Client(options) {
+ this.consumerKey = options.consumerKey;
+ this.consumerSecret = options.consumerSecret;
+
+ var oAuth = new OAuth(
+ 'http://twitter.com/oauth/request_token',
+ 'http://twitter.com/oauth/access_token',
+ this.consumerKey,
+ this.consumerSecret,
+ '1.0',
+ null,
+ 'HMAC-SHA1',
+ null,
+ {
+ 'Accept': '*/*',
+ 'Connection': 'close',
+ 'User-Agent': 'twitter.js ' + module.parent.version
+ }
+ );
+
+ var API_BASE = 'http://api.twitter.com/1';
+
+ var callbackWrapper = function(callback) {
+ return function (error, data) {
+ if (error) {
+ callback(error);
+ } else {
+ try {
+ callback(null, JSON.parse(data));
+ } catch (error) {
+ callback(error);
+ }
+ }
+ };
+ }
+
+ var API = function(token) {
+ return {
+ token: token,
+
+ get: function(path, params, callback) {
+ oAuth.get(API_BASE + path, this.token.oauth_token, this.token.oauth_token_secret, params, null, callbackWrapper(callback));
+ },
+
+ post: function(path, params, callback) {
+ oAuth.post(API_BASE + path, this.token.oauth_token, this.token.oauth_token_secret, params, null, callbackWrapper(calback));
+ }
+ }
+ }
+
+ this.authorize = function(request, response, callback) {
+ var url = URL.parse(request.url, true);
+
+ if (!request.session.auth) { request.session.auth = {} };
+
+ request.session.auth.callback_url = 'http://' + request.headers.host + request.url;
+
+ if (request.session.auth.token) {
+ callback(null, new API(request.session.auth.token));
+ } else if (url.query && url.query.oauth_token && request.session.auth.twitter_oauth_token_secret) {
+ oAuth.getOAuthAccessToken(
+ url.query.oauth_token,
+ request.session.auth.twitter_oauth_token_secret,
+ url.query.oauth_verifier,
+ function (error, oauth_token, oauth_token_secret, params) {
+ if (error) {
+ callback(error);
+ } else {
+ request.session.auth.token = {
+ oauth_token: oauth_token,
+ oauth_token_secret: oauth_token_secret
+ };
+
+ callback(error, new API(request.session.auth.token));
+ }
+ }
+ );
+ } else {
+ oAuth._authorize_callback = request.session.auth.callback_url;
+
+ oAuth.getOAuthRequestToken(
+ function (error, oauth_token, oauth_token_secret, oauth_authorize_url, params) {
+ if (error) {
+ callback(error);
+ } else {
+ request.session.twitter_redirect_url = request.url;
+ request.session.auth.twitter_oauth_token_secret = oauth_token_secret;
+ request.session.auth.twitter_oauth_token = oauth_token;
+ response.redirect('http://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token);
+ }
+ }
+ );
+ }
+ };
+}
20 package.json
@@ -0,0 +1,20 @@
+{
+ "name": "twitter-connect",
+ "description": "Twitter authentication for connect apps",
+ "main": "./index",
+ "author": "David Dollar",
+ "version": "0.0.1",
+ "repository" : {
+ "type": "git",
+ "url": "http://github.com/caolan/cookie-sessions.git"
+ },
+ "bugs": {
+ "web": "http://github.com/caolan/cookie-sessions/issues"
+ },
+ "licenses" : [
+ {
+ "type": "MIT",
+ "url": "http://github.com/caolan/cookie-sessions/raw/master/LICENSE"
+ }
+ ]
+}

0 comments on commit af9bb45

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