Skip to content

Commit ea54550

Browse files
committed
Merge pull request AvianFlu#94 from gazzer82/master
Implemented application-only authentication
2 parents 253076c + 6833c8a commit ea54550

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ You will need valid Twitter developer credentials in the form of a set of consum
3232

3333
```javascript
3434
var Twitter = require('twitter');
35+
```
36+
37+
## For User based authetication:
3538

39+
```javascript
3640
var client = new Twitter({
3741
consumer_key: '',
3842
consumer_secret: '',
@@ -51,6 +55,31 @@ var client = new Twitter({
5155
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
5256
});
5357
```
58+
## For Application Only based authetication:
59+
60+
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.
61+
62+
```javascript
63+
var client = new Twitter({
64+
consumer_key: '',
65+
consumer_secret: '',
66+
bearer_token: ''
67+
});
68+
```
69+
70+
Add your credentials accordingly. I would use environment variables to keep your private info safe. So something like:
71+
72+
```javascript
73+
var client = new Twitter({
74+
consumer_key: process.env.TWITTER_CONSUMER_KEY,
75+
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
76+
bearer_token: process.env.TWITTER_BEARER_TOKEN,
77+
});
78+
```
79+
80+
NB - You will not have access to all endpoints whilst using Application Only authentication, but you will have access to higher API limits.
81+
82+
## Requests
5483

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

lib/twitter.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function Twitter (options) {
2323
consumer_secret: null,
2424
access_token_key: null,
2525
access_token_secret: null,
26+
bearer_token: null,
2627
rest_base: 'https://api.twitter.com/1.1',
2728
stream_base: 'https://stream.twitter.com/1.1',
2829
user_stream_base: 'https://userstream.twitter.com/1.1',
@@ -32,26 +33,42 @@ function Twitter (options) {
3233
headers: {
3334
'Accept': '*/*',
3435
'Connection': 'close',
35-
'User-Agent': 'node-twitter/' + VERSION
36+
'User-Agent': 'node-twitter/' + VERSION,
3637
}
3738
}
3839
}, options);
39-
40-
// Build a request object
41-
this.request = request.defaults(
42-
extend(
43-
// Pass the client submitted request options
44-
this.options.request_options,
45-
{
46-
oauth: {
47-
consumer_key: this.options.consumer_key,
48-
consumer_secret: this.options.consumer_secret,
49-
token: this.options.access_token_key,
50-
token_secret: this.options.access_token_secret
40+
//Check to see if we are going to use User Authentication or Application Authetication
41+
if (this.options.bearer_token){
42+
//Ok we have a bearer token, so going with application-only auth
43+
// Build a request object
44+
this.request = request.defaults(
45+
extend(
46+
//Pass the client submitted request options
47+
this.options.request_options,
48+
{
49+
headers: {
50+
Authorization: 'Bearer ' + this.options.bearer_token
51+
}
52+
}
53+
)
54+
);
55+
} else {
56+
//No bearer token detected so defaulting to user auth
57+
this.request = request.defaults(
58+
extend(
59+
//Pass the client submitted request options
60+
this.options.request_options,
61+
{
62+
oauth: {
63+
consumer_key: this.options.consumer_key,
64+
consumer_secret: this.options.consumer_key,
65+
token: this.options.access_token_key,
66+
token_secret: this.options.access_token_secret
67+
}
5168
}
52-
}
53-
)
54-
);
69+
)
70+
);
71+
}
5572
}
5673

5774
Twitter.prototype.__buildEndpoint = function(path, base) {

test/twitter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Twitter', function() {
1919
consumer_secret: null,
2020
access_token_key: null,
2121
access_token_secret: null,
22+
bearer_token: null,
2223
rest_base: 'https://api.twitter.com/1.1',
2324
stream_base: 'https://stream.twitter.com/1.1',
2425
user_stream_base: 'https://userstream.twitter.com/1.1',

0 commit comments

Comments
 (0)