-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.js
130 lines (118 loc) · 3.5 KB
/
api.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
var router = require('express').Router();
var request = require('request');
var OAuth2 = require('oauth').OAuth2;
var uberApiUrl = 'https://sandbox-api.uber.com/v1/';
var uberServerToken = process.env.UBER_SERVER_TOKEN;
var uberClientID = process.env.UBER_CLIENT_ID;
var uberClientSecret = process.env.UBER_CLIENT_SECRET;
// use this if running on external host
// var serverUrl = 'http://' + require("os").hostname() + ':' + ( process.env.PORT || 3000 );
var serverUrl = 'http://localhost:' + ( process.env.PORT || 3000 );
var oauth2 = new OAuth2(
uberClientID,
uberClientSecret,
'https://login.uber.com/',
'oauth/authorize',
'oauth/token',
null);
router.get('/estimates/price', function(req, res){
var source = JSON.parse(req.query.source);
var destination = JSON.parse(req.query.destination);
// create http request to uber api
request.get({
url : uberApiUrl + 'estimates/price',
strictSSL: false,
qs : {
server_token : uberServerToken,
start_latitude : source.lat,
start_longitude : source.lng,
end_latitude : destination.lat,
end_longitude : destination.lng
}
}, function(err, response, body){
if(err){
return res.json(err);
}
res.json(body);
});
});
router.post('/get_ride', function(req, res){
if( !req.body.hasOwnProperty('auth_token') ){
return res.json({
success : false,
code : 401,
popupUrl : getAuthorizeUrl()
});
}
var uberRequest = {
start_latitude : req.body.source.lat,
start_longitude : req.body.source.lng,
end_latitude : req.body.destination.lat,
end_longitude : req.body.destination.lng,
product_id : req.body.product_id
};
// create http request to uber api
request.post({
url : uberApiUrl + 'requests',
json : uberRequest,
strictSSL: false,
auth : {
bearer : req.body.auth_token
}
}, function(err, response, body){
if(err){
return res.json(err);
}
body.success = true;
res.json(body);
});
});
// from redirect after auth
router.get('/oauth/cb', function(req, res){
var code = req.query.code;
oauth2.getOAuthAccessToken(
code,
{ // NOT IN THE UBER DOCS
grant_type: 'authorization_code',
redirect_uri: serverUrl+'/api/oauth/cb'
},
function (err, access_token, refresh_token, results){
if(err){
console.log(err);
if(err.data){
res.end(err.data);
}else{
res.json(err);
}
} else if(results.error) {
console.log(results.error);
res.json(results.error);
} else {
// got token, send back to client
// POPUP Blocker must be disabled, or find workaround, or use redirect instead
res.send(closeAndRedirectScript(access_token));
}
});
});
function closeAndRedirectScript(access_token) {
return '<script> \
if (window.opener != null && !window.opener.closed){ \
window.opener.location = "'+redirectAccessTokenUrl(access_token)+'"; \
window.close(); \
}else{ \
document.write("Pop-up blocker prevented proper authorization process. Please disable and re-authorize."); \
} \
</script>';
}
function redirectAccessTokenUrl(access_token) {
return serverUrl + '#store-auth-token/' + access_token;
}
function getAuthorizeUrl(){
return oauth2.getAuthorizeUrl({
redirect_uri: serverUrl + '/api/oauth/cb',
scope: ['request'],
state: 'authorizing',
response_type: 'code'
});
}
module.exports = router;