Skip to content
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
69 changes: 44 additions & 25 deletions lib/Twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -163,8 +183,7 @@ Twitter.prototype.getCustomApiCall = function (url, params, error, success) {
};

Twitter.prototype.postCustomApiCall = function (url, params, error, success) {
var path = url + this.buildQS(params);
var url = this.baseUrl + path;
var url = this.baseUrl + url;
this.doPost(url, params, error, success);
};

Expand Down