-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
167 lines (129 loc) · 4.23 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
require('dotenv').config();
/**
* Require the dependencies
* @type {*|createApplication}
*/
var express = require('express');
var app = express();
var path = require('path');
var OAuthClient = require('intuit-oauth');
var bodyParser = require('body-parser');
var ngrok = (process.env.NGROK_ENABLED==="true") ? require('ngrok'):null;
/**
* Configure View and Handlebars
*/
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, '/public')));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.json())
var urlencodedParser = bodyParser.urlencoded({ extended: false });
/**
* App Variables
* @type {null}
*/
var oauth2_token_json = null,
redirectUri = '';
/**
* Instantiate new Client
* @type {OAuthClient}
*/
var oauthClient = null;
/**
* Home Route
*/
app.get('/', function(req, res) {
res.render('index');
});
/**
* Get the AuthorizeUri
*/
app.get('/authUri', urlencodedParser, function(req,res) {
oauthClient = new OAuthClient({
clientId: req.query.json.clientId,
clientSecret: req.query.json.clientSecret,
environment: req.query.json.environment,
redirectUri: req.query.json.redirectUri
});
var authUri = oauthClient.authorizeUri({scope:[OAuthClient.scopes.Accounting],state:'intuit-test'});
res.send(authUri);
});
/**
* Handle the callback to extract the `Auth Code` and exchange them for `Bearer-Tokens`
*/
app.get('/callback', function(req, res) {
oauthClient.createToken(req.url)
.then(function(authResponse) {
oauth2_token_json = JSON.stringify(authResponse.getJson(), null,2);
})
.catch(function(e) {
console.error(e);
});
res.send('');
});
/**
* Display the token : CAUTION : JUST for sample purposes
*/
app.get('/retrieveToken', function(req, res) {
res.send(oauth2_token_json);
});
/**
* Refresh the access-token
*/
app.get('/refreshAccessToken', function(req,res){
oauthClient.refresh()
.then(function(authResponse){
console.log('The Refresh Token is '+ JSON.stringify(authResponse.getJson()));
oauth2_token_json = JSON.stringify(authResponse.getJson(), null,2);
res.send(oauth2_token_json);
})
.catch(function(e) {
console.error(e);
});
});
/**
* getCompanyInfo ()
*/
app.get('/getCompanyInfo', function(req,res){
var companyID = oauthClient.getToken().realmId;
var url = oauthClient.environment == 'sandbox' ? OAuthClient.environment.sandbox : OAuthClient.environment.production ;
oauthClient.makeApiCall({url: url + 'v3/company/' + companyID +'/companyinfo/' + companyID})
.then(function(authResponse){
console.log("The response for API call is :"+JSON.stringify(authResponse));
res.send(JSON.parse(authResponse.text()));
})
.catch(function(e) {
console.error(e);
});
});
/**
* Start server on HTTP (will use ngrok for HTTPS forwarding)
*/
const server = app.listen(process.env.PORT || 8000, () => {
console.log(`💻 Server listening on port ${server.address().port}`);
if(!ngrok){
redirectUri = `${server.address().port}` + '/callback';
console.log(`💳 See the Sample App in your browser : ` + 'http://localhost:' + `${server.address().port}`);
console.log(`💳 Copy this into Redirect URI on the browser : ` + 'http://localhost:' + `${server.address().port}` + '/callback');
console.log(`💻 Make Sure this redirect URI is also copied on your app in : https://developer.intuit.com`);
}
});
/**
* Optional : If NGROK is enabled
*/
if (ngrok) {
console.log("NGROK Enabled");
ngrok.connect({addr: process.env.PORT || 8000}, (err, url) => {
if (err) {
process.exit(1);
}
else {
redirectUri = url + '/callback';
console.log(`💳 See the Sample App in your browser: ${url}`);
console.log(`💳 Copy and paste this Redirect URI on the browser : ${redirectUri}`);
console.log(`💻 Make Sure this redirect URI is also copied on your app in : https://developer.intuit.com`);
}
}
);
}