forked from drouillard/sample-ntwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (124 loc) · 4.6 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Module dependencies.
Added
1. ntwitter - package for using Twitter API
2. url - used to parse out different parts of URLs
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, ntwitter = require('ntwitter')
, url = require('url');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('secretsession'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
/**
* Above this line are Express Defaults.
*/
app.get('/', routes.index);
/**
* This demonstrates a use case where the Application itself is making all of the API calls on its
* own behalf.
*/
app.get('/single', function(req, res){
console.log("Entering Single User Example...");
/* Be sure to include all 4 tokens.
* Default keys don't work. I am leaving them to make it easier to compare to screenshots found at
* https://github.com/drouillard/sample-ntwitter
* NOTE: In a real application do not embedd your keys into the source code
* TODO: Fill in your Application information here
*/
var twit = new ntwitter({
consumer_key: 'EibJRBO1NGcJvpxAIZnXjQ',
consumer_secret: '4etBzAdBXqe2uzHR4BN1fut1DBeGeyR0PEFuLCB1YzU',
access_token_key: '767069725-rGlwY6ARanWWJE5mUpHgOYzOoL3IL0xYeQZQ1Ava',
access_token_secret: 'yF2YtlRzpBmTmAeMAFmYnDh1vgwwLGqKC8G80s0crw'
});
twit
.verifyCredentials(function (err, data) {
console.log("Verifying Credentials...");
if(err)
console.log("Verification failed : " + err)
})
.getHomeTimeline('',
function (err, data) {
console.log("Timeline Data Returned....");
// console.log(data);
var view_data = {
"timeline" : JSON.stringify(data)
}
console.log("Exiting Controller.");
res.render('single',view_data);
});
});
app.get('/signin_with_twitter', function(req, res){
console.log("Entering Sign-in With Twitter Example...");
/**
* Include only Application Specific Tokens. User Sign-in with Twitter to get Ouath tokens
* Default keys don't work. I am leaving them to make it easier to compare to screenshots found at
* https://github.com/drouillard/sample-ntwitter
* NOTE: In a real application do not embedd your keys into the source code
* TODO: Fill in your Application information here
*/
var twit = new ntwitter({
consumer_key: 'EibJRBO1NGcJvpxAIZnXjQ',
consumer_secret: '4etBzAdBXqe2uzHR4BN1fut1DBeGeyR0PEFuLCB1YzU'});
var path = url.parse(req.url, true);
twit.login(path.pathname,"/twitter_callback")(req,res);
/**
* Do NOT include any sort of template rendering here
* If you do so, it will prevent the redirect to Twitter from happening
* res.render('do_not_enable ');
*/
});
app.get('/twitter_callback', function(req, res){
console.log("Sucessfully Authenticated with Twitter...");
/**
* Include only Application Specific Tokens. User Sign-in with Twitter to get Ouath Tokens
* Default keys don't work. I am leaving them to make it easier to compare to screenshots found at
* https://github.com/drouillard/sample-ntwitter
* NOTE: In a real application do not embedd your keys into the source code
* TODO: Fill in your Application information here
*/
var twit = new ntwitter({
consumer_key: 'EibJRBO1NGcJvpxAIZnXjQ',
consumer_secret: '4etBzAdBXqe2uzHR4BN1fut1DBeGeyR0PEFuLCB1YzU'});
twit.gatekeeper()(req,res,function(){
req_cookie = twit.cookie(req);
twit.options.access_token_key = req_cookie.access_token_key;
twit.options.access_token_secret = req_cookie.access_token_secret;
twit.verifyCredentials(function (err, data) {
console.log("Verifying Credentials...");
if(err)
console.log("Verification failed : " + err)
})
.getHomeTimeline('',
function (err, data) {
console.log("Timeline Data Returned...");
// console.log(data);
var view_data = {
"timeline" : JSON.stringify(data)
}
console.log("Exiting Controller.");
res.render('signin_with_twitter',view_data);
});
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});