diff --git a/README.md b/README.md index cde27db..3c5ca24 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,22 @@ You need to create a [Twitter app](https://dev.twitter.com/apps) to use the API. "callBackUrl": "XXX" } - // make a directory in the root folder of your project called data - // copy the node_modules/twitter-node-client/twitter_config file over into data/twitter_config` - // Open `data/twitter_config` and supply your applications `consumerKey`, 'consumerSecret', 'accessToken', 'accessTokenSecret', 'callBackUrl' to the appropriate fields in your data/twitter_config file - + // The twitter data can be then added via process environment variables or passed in as an object to the module. + // the preferred method is to use process environment variables to keep your keys out of github. + // If you include it on your process environment variables you can just do var twitter = new Twitter(); + // and the library will look on environment variables for your keys. + // alternatively you can + // pass a config object that will look like this below, but remember to not push these key containing files to + // github as they can be easily compromised. + // var config = { + // consumerKey='xxx' + // consumerSecret='xxx' + // accessToken='xxx' + // accessTokenSecret='xxx' + // callBackUrl='xxx' + // }; + // var twitter = new Twitter(config); //Example calls diff --git a/lib/Twitter.js b/lib/Twitter.js index 2da39bc..17b4a17 100644 --- a/lib/Twitter.js +++ b/lib/Twitter.js @@ -6,31 +6,51 @@ var DEBUG = (process.env.DEBUG !== undefined); var OAuth = require('oauth').OAuth; var qs = require('qs'); - -function Twitter() { - var configPath = "data/twitter_config"; - try { - var config = process.env; - - this.consumerKey = config.twitterConsumerKey; - this.consumerSecret = config.twitterConsumerSecret; - this.accessToken = config.twitterAccessToken; - this.accessTokenSecret = config.twitterAccessTokenSecret; - this.callBackUrl = config.twitterCallBackUrl; - this.baseUrl = 'https://api.twitter.com/1.1'; - this.oauth = new OAuth( - 'https://api.twitter.com/oauth/request_token', - 'https://api.twitter.com/oauth/access_token', - this.consumerKey, - this.consumerSecret, - '1.0', - this.callBackUrl, - 'HMAC-SHA1' - ); - } catch (err) { - //console.log(err) +function Twitter(config) { + try { + var processEnvConfig = process.env; + + this.consumerKey = processEnvConfig.consumerKey; + this.consumerSecret = processEnvConfig.consumerSecret; + this.accessToken = processEnvConfig.accessToken; + this.accessTokenSecret = processEnvConfig.accessTokenSecret; + this.callBackUrl = processEnvConfig.callBackUrl; + this.baseUrl = 'https://api.twitter.com/1.1'; + this.oauth = new OAuth( + 'https://api.twitter.com/oauth/request_token', + 'https://api.twitter.com/oauth/access_token', + this.consumerKey, + this.consumerSecret, + '1.0', + this.callBackUrl, + 'HMAC-SHA1' + ); + if(typeof this.consumerKey === 'undefined'){ + throw new Error('missing consumer keys on process environment trying for config object'); + } + } + catch (err) { + if(config){ + this.consumerKey = config.consumerKey; + this.consumerSecret = config.consumerSecret; + this.accessToken = config.accessToken; + this.accessTokenSecret = config.accessTokenSecret; + this.callBackUrl = config.callBackUrl; + this.baseUrl = 'https://api.twitter.com/1.1'; + this.oauth = new OAuth( + 'https://api.twitter.com/oauth/request_token', + 'https://api.twitter.com/oauth/access_token', + this.consumerKey, + this.consumerSecret, + '1.0', + this.callBackUrl, + 'HMAC-SHA1' + ); + } + else { console.log("no 'data/twitter_config' file, continuing without..."); } + } } Twitter.prototype.getOAuthRequestToken = function (next) {