DropshippingApp-Get-Allegro.../routes/index.js

77 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-09-01 20:20:03 +00:00
const express = require('express');
const router = express.Router();
const axios = require('axios')
const {google} = require('googleapis')
2020-09-01 20:20:03 +00:00
// TAKEN FROM OUR ALLEGRO APP CONFIGURATION --> https://apps.developer.allegro.pl/
const ALLEGRO_AUTH_TOKEN_URL = "https://allegro.pl/auth/oauth/token"
const REDIRECT_URI = "https://dropshipping.xaos.ninja"
2020-10-21 18:34:54 +00:00
const CLIENT_ID = "9426c5af899d4de9a513242a8bd9c955"
const CLIENT_SECRET = "Wxw96FWT2FNAwkiu39ABhSJqeJX4JJrPpFis6q7kDYGy240cS54Aa0mIzOVgj3jG"
2020-09-01 20:20:03 +00:00
// GOOGLE DOCS API
const GOOGLE_CLIENT_ID = "811597362147-fekutrahfqdovhl073pdmevpjnctas2t.apps.googleusercontent.com"
const GOOGLE_CLIENT_SECRET = "eHig0XC6WhYqaFRX5Mcoxqf9"
const SCOPE = ["https://www.googleapis.com/auth/spreadsheets"]
const oAuth2Client = new google.auth.OAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
2020-09-01 20:20:03 +00:00
/* GET home page. */
router.get('/', function (req, res, next) {
const query = req?.query
2020-09-01 20:20:03 +00:00
const code = req?.query?.code
// when we are redirected from Allegro
if (query.code && !query.scope) {
2020-09-01 20:20:03 +00:00
const credentials = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64")
axios.post(ALLEGRO_AUTH_TOKEN_URL, null, {
params: {
grant_type: "authorization_code",
code: code,
redirect_uri: REDIRECT_URI
},
headers: {"Authorization": `Basic ${credentials}`}
}).then(response => {
const {access_token, refresh_token} = response.data
res.render('index', {
allegro_access_token: access_token,
allegro_refresh_token: refresh_token,
google_auth_url: "",
google_tokens_json: "",
});
2020-09-01 20:20:03 +00:00
})
//when we are redirected from Google
} else if (query.code && query.scope) {
oAuth2Client.getToken({code: code, redirect_uri: REDIRECT_URI})
.then(response => {
const google_tokens_json = JSON.stringify(response.tokens)
res.render('index', {
allegro_access_token: "",
allegro_refresh_token: "",
google_auth_url: "",
google_tokens_json: google_tokens_json
})
})
//when we just enter the page
2020-09-01 20:20:03 +00:00
} else {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPE,
redirect_uri: REDIRECT_URI,
prompt: 'consent'
})
res.render('index', {
allegro_access_token: "",
allegro_refresh_token: "",
google_auth_url: authUrl,
google_tokens_json: "",
});
2020-09-01 20:20:03 +00:00
}
});
module.exports = router;