Skip to content

Commit

Permalink
Merge pull request AvianFlu#94 from gazzer82/master
Browse files Browse the repository at this point in the history
Implemented application-only authentication
  • Loading branch information
desmondmorris committed Jun 11, 2015
2 parents 253076c + 6833c8a commit ea54550
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ You will need valid Twitter developer credentials in the form of a set of consum

```javascript
var Twitter = require('twitter');
```

## For User based authetication:

```javascript
var client = new Twitter({
consumer_key: '',
consumer_secret: '',
Expand All @@ -51,6 +55,31 @@ var client = new Twitter({
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
```
## For Application Only based authetication:

You will need to fetch a bearer token from Twitter as documented [Here](https://dev.twitter.com/oauth/application-only), once you have it you can use it as follows.

```javascript
var client = new Twitter({
consumer_key: '',
consumer_secret: '',
bearer_token: ''
});
```

Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:

```javascript
var client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
bearer_token: process.env.TWITTER_BEARER_TOKEN,
});
```

NB - You will not have access to all endpoints whilst using Application Only authentication, but you will have access to higher API limits.

## Requests

You now have the ability to make GET and POST requests against the API via the convenience methods.

Expand Down
49 changes: 33 additions & 16 deletions lib/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Twitter (options) {
consumer_secret: null,
access_token_key: null,
access_token_secret: null,
bearer_token: null,
rest_base: 'https://api.twitter.com/1.1',
stream_base: 'https://stream.twitter.com/1.1',
user_stream_base: 'https://userstream.twitter.com/1.1',
Expand All @@ -32,26 +33,42 @@ function Twitter (options) {
headers: {
'Accept': '*/*',
'Connection': 'close',
'User-Agent': 'node-twitter/' + VERSION
'User-Agent': 'node-twitter/' + VERSION,
}
}
}, options);

// Build a request object
this.request = request.defaults(
extend(
// Pass the client submitted request options
this.options.request_options,
{
oauth: {
consumer_key: this.options.consumer_key,
consumer_secret: this.options.consumer_secret,
token: this.options.access_token_key,
token_secret: this.options.access_token_secret
//Check to see if we are going to use User Authentication or Application Authetication
if (this.options.bearer_token){
//Ok we have a bearer token, so going with application-only auth
// Build a request object
this.request = request.defaults(
extend(
//Pass the client submitted request options
this.options.request_options,
{
headers: {
Authorization: 'Bearer ' + this.options.bearer_token
}
}
)
);
} else {
//No bearer token detected so defaulting to user auth
this.request = request.defaults(
extend(
//Pass the client submitted request options
this.options.request_options,
{
oauth: {
consumer_key: this.options.consumer_key,
consumer_secret: this.options.consumer_key,
token: this.options.access_token_key,
token_secret: this.options.access_token_secret
}
}
}
)
);
)
);
}
}

Twitter.prototype.__buildEndpoint = function(path, base) {
Expand Down
1 change: 1 addition & 0 deletions test/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Twitter', function() {
consumer_secret: null,
access_token_key: null,
access_token_secret: null,
bearer_token: null,
rest_base: 'https://api.twitter.com/1.1',
stream_base: 'https://stream.twitter.com/1.1',
user_stream_base: 'https://userstream.twitter.com/1.1',
Expand Down

0 comments on commit ea54550

Please sign in to comment.