-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevServer.js
More file actions
119 lines (105 loc) · 3.41 KB
/
Copy pathdevServer.js
File metadata and controls
119 lines (105 loc) · 3.41 KB
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
var http = require('http');
var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require('./webpack.config.dev');
var ig = require('instagram-node').instagram();
var app = express();
var compiler = webpack(config);
var redirect_uri = 'http://localhost:8000/handleauth';
var accessToken;
var userId;
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
//app.use(express.static(__dirname + '/public'));
//ig.use({ access_token: '1100019683.842ad45.7adf1739782242df86f322df4d7f4f89' });
// Every call to `ig.use()` overrides the `client_id/client_secret`
ig.use({
client_id: '842ad45977294eb98b53c5f9ddce023d',
client_secret: 'e2d3f444594d4bd192c88e88254402a1'
});
//access_token=1100019683.842ad45.7adf1739782242df86f322df4d7f4f89
exports.authorize_user = function(req, res) {
if(typeof accessToken === 'undefined')
res.redirect(ig.get_authorization_url(redirect_uri, { scope: ['public_content', 'likes'], state: 'true' }));
else
res.redirect('/gallery');
};
exports.handleauth = function(req, res) {
ig.authorize_user(req.query.code, redirect_uri, function(err, result) {
if (err) {
console.log(err.body);
res.send("Access denied");
} else {
accessToken = result.access_token;
userId = accessToken.split('.')[0];
ig.use({
access_token : accessToken
});
console.log('Your Access token is ' + result.access_token);
//res.send('Authorized access');
res.redirect('/gallery');
}
});
};
// This is where you would initially send users to authorize
app.get('/authorize_user', exports.authorize_user);
// This is your redirect URI
app.get('/handleauth', exports.handleauth);
app.get('/getUserInfo', function(req, res){
ig.user(userId, function(err, result, remaining, limit) {
if(err) {
console.log("Error: " + err);
res.send(500)
}
res.send(result);
});
});
app.get('/getMedia', function(req, res){
var total = 0;
var hdl = function(err, result, pagination, remaining, limit) {
if(err) {
console.log("Error: ", err.message, err.stack);
} else {
res.send(result);
}
};
ig.user_media_recent(userId, { min_timestamp: 0 }, hdl);
});
// app.get('/getMedia', function(req, res){
// var total = 0;
// var hdl = function(err, result, pagination, remaining, limit) {
// if(err) {
// console.log("Error: ", err.message, err.stack);
// } else {
// console.log('Pagination: ', pagination.next);
// console.log('remaining: ', remaining);
// console.log('limit: ', limit);
// result.forEach(function(media){
// total ++;
// console.log(media.id);
// });
// if(pagination.next){
// pagination.next(hdl);
// }else {
// console.log('Total media: ', total);
// res.send(result);
// }
// }
// };
// //create a new instance of the use method which contains the access token gotten
// ig.user_media_recent(userId, { count:5, max_id: '15535441_1226698387421563'}, hdl);
// });
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(8000, '0.0.0.0', function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:8000');
});