const express = require('express'); const axios = require('axios'); const app = express(); // Substitua pelos seus valores reais const CLIENT_ID = '51b5db07-1528-4231-aa0a-d347588f0449'; const CLIENT_SECRET = 'ofOdMU6sFosBY2J4EH966kVok9aGaHmcYxeYaJ0k8TXDmaibFLngeZqpsa17roa3'; // Substitua pelo seu segredo de cliente const REDIRECT_URI = 'https://d16.desafio16dias.com.br/kommo_requisition/callback'; // Endpoint para iniciar o processo de autorização app.get('/auth', (req, res) => { const authUrl = `https://www.kommo.com/oauth?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=leads%20contacts%20tasks`; res.redirect(authUrl); }); // Endpoint de redirecionamento para capturar o código de autorização app.get('/kommo_requisition/callback', async (req, res) => { const authorizationCode = req.query.code; try { const response = await axios.post('https://www.kommo.com/oauth2/token', { code: authorizationCode, client_id: CLIENT_ID, client_secret: CLIENT_SECRET, redirect_uri: REDIRECT_URI, grant_type: 'authorization_code' }); const { access_token, refresh_token } = response.data; res.send(`Access Token: ${access_token}
Refresh Token: ${refresh_token}`); } catch (error) { res.send('Error exchanging authorization code for tokens'); } }); // Inicia o servidor na porta 3000 app.listen(3000, () => { console.log('Server is running on port 3000'); });