Skip to content

Migrate to request to axios and moment with Luxon #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 86 additions & 27 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var http = require('http'),
port = process.env.PORT || 3000,
request = require('request'),
axios = require('axios'),
OAuth = require('oauth-1.0a'),
crypto = require('crypto'),
qs = require('querystring'),
util = require('util'),
bodyParser = require('body-parser'),
Expand Down Expand Up @@ -37,38 +39,92 @@ app.get('/start', function(req, res) {
})

app.get('/requestToken', function(req, res) {
var postBody = {

// Create an OAuth 1.0 instance
var oauth = OAuth({
consumer: {
key: consumerKey,
secret: consumerSecret
},
signature_method: 'HMAC-SHA1',
hash_function(baseString, key) {
return crypto.createHmac('sha1', key).update(baseString).digest('base64');
}
});

var requestData = {
url: QuickBooks.REQUEST_TOKEN_URL,
oauth: {
callback: 'http://localhost:' + port + '/callback/',
consumer_key: consumerKey,
consumer_secret: consumerSecret
method: 'POST',
data: {
oauth_callback: 'http://localhost:' + port + '/callback/'
}
}
request.post(postBody, function (e, r, data) {
var requestToken = qs.parse(data)
req.session.oauth_token_secret = requestToken.oauth_token_secret
console.log(requestToken)
res.redirect(QuickBooks.APP_CENTER_URL + requestToken.oauth_token)
};

var headers = oauth.toHeader(oauth.authorize(requestData, null));

axios({
url: requestData.url,
method: requestData.method,
headers: {
...headers,
'Content-Type': 'application/x-www-form-urlencoded',
},
data: new URLSearchParams(requestData.data).toString()
})
.then(function(response) {
var requestToken = qs.parse(response.data);
req.session.oauth_token_secret = requestToken.oauth_token_secret;
console.log(requestToken);
res.redirect(QuickBooks.APP_CENTER_URL + requestToken.oauth_token);
})
.catch(function(error) {
console.log(error);
});
})

app.get('/callback', function(req, res) {
var postBody = {
url: QuickBooks.ACCESS_TOKEN_URL,
oauth: {
consumer_key: consumerKey,
consumer_secret: consumerSecret,
token: req.query.oauth_token,
token_secret: req.session.oauth_token_secret,
verifier: req.query.oauth_verifier,
realmId: req.query.realmId
// Create an OAuth 1.0 instance
var oauth = OAuth({
consumer: {
key: consumerKey,
secret: consumerSecret
},
signature_method: 'HMAC-SHA1',
hash_function(baseString, key) {
return crypto.createHmac('sha1', key).update(baseString).digest('base64');
}
}
request.post(postBody, function (e, r, data) {
var accessToken = qs.parse(data)
console.log(accessToken)
console.log(postBody.oauth.realmId)
});

var requestData = {
url: QuickBooks.REQUEST_TOKEN_URL,
method: 'POST',
data: {
oauth_callback: 'http://localhost:' + port + '/callback/'
}
};

var token = {
token: req.query.oauth_token,
token_secret: req.session.oauth_token_secret,
verifier: req.query.oauth_verifier,
realmId: req.query.realmId
};

var headers = oauth.toHeader(oauth.authorize(requestData, token));

axios({
url: QuickBooks.ACCESS_TOKEN_URL,
method: requestData.method,
headers: {
...headers,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: new URLSearchParams(requestData.data).toString()
})
.then(function(response) {
var accessToken = qs.parse(response.data);
console.log(accessToken);
console.log(postBody.oauth.realmId);

// save the access token somewhere on behalf of the logged in user
qbo = new QuickBooks(consumerKey,
Expand All @@ -86,5 +142,8 @@ app.get('/callback', function(req, res) {
})
})
})
res.send('<!DOCTYPE html><html lang="en"><head></head><body><script>window.opener.location.reload(); window.close();</script></body></html>')
.catch(function(error) {
console.log(error);
});
res.send('<!DOCTYPE html><html lang="en"><head></head><body><script>window.opener.location.reload(); window.close();</script></body></html>');
})
Loading