Answer the question
In order to leave comments, you need to log in
How to pass promise value from one file to another?
There is a server file and an authentication file, I need to pass the received value from the authentication file to the server file. I use axios and primis technology. I confess: I understand almost nothing about promises and asynchrony, but I need quick help.
Below are these files.
server.js
const path = require('path');
const express = require('express');
const Axios = require('axios');
const bodyParser = require('body-parser');
const querystring = require('querystring');
let app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
const config = require('./config');
const PORT = config.credentials.PORT;
var FORGE_CLIENT_ID = 'myID';
var FORGE_CLIENT_SECRET = 'mySecret';
const scopes = 'data:read data:write data:create bucket:create bucket:read';
app.use('/api/forge', require('./routes/oauth'));
app.use((err, req, res, next) => {
console.error(err);
res.status(err.statusCode).json(err);
});
let server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`) });
const path = require('path');
const express = require('express');
const Axios = require('axios');
const bodyParser = require('body-parser');
const querystring = require('querystring');
const config = require('../config');
var FORGE_CLIENT_ID = 'myId';
var FORGE_CLIENT_SECRET = 'mySecret';
const scopes = 'data:read data:write data:create bucket:create bucket:read';
let router = express.Router();
router.use(bodyParser.json());
let access_token = '';
router.get('/oauth', function (req, res) {
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
data: querystring.stringify({
client_id: FORGE_CLIENT_ID,
client_secret: FORGE_CLIENT_SECRET,
grant_type: 'client_credentials',
scope: scopes
})
})
.then(function (response) {
access_token = response.data.access_token;
console.log(response);
res.redirect('/api/forge/datamanagement/bucket/create');
})
.catch(function (error) {
// Failed
console.error(error);
res.send('Failed to authenticate');
});
});
module.exports = router;
Answer the question
In order to leave comments, you need to log in
The principle is this.
You write an oauth.js module that looks like this:
const Axios = require('axios');
const querystring = require('querystring');
var FORGE_CLIENT_ID = 'myId';
var FORGE_CLIENT_SECRET = 'mySecret';
const scopes = 'data:read data:write data:create bucket:create bucket:read';
module.exports = function() {
return Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
data: querystring.stringify({
client_id: FORGE_CLIENT_ID,
client_secret: FORGE_CLIENT_SECRET,
grant_type: 'client_credentials',
scope: scopes
})
});
};
require('./oauth')
.then(function (response) {
access_token = response.data.access_token;
})
.catch(...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question