mirror of
https://github.com/FranP-code/Open-Telegram-to-Notion-Backend.git
synced 2025-10-12 23:52:54 +00:00
API structure done
This commit is contained in:
35
src/index.js
Normal file
35
src/index.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const Express = require('express')
|
||||
const app = Express()
|
||||
const port = (process.env.PORT || 3030)
|
||||
require('dotenv').config()
|
||||
|
||||
//Configure Handlebars
|
||||
const hbs = require('hbs')
|
||||
hbs.registerPartials(__dirname + '/../views/partials')
|
||||
app.set('view engine', 'hbs')
|
||||
|
||||
//Use public folder
|
||||
app.use(Express.static('public'));
|
||||
|
||||
//Create master route
|
||||
const masterRoute = Express.Router()
|
||||
|
||||
app.use('/api/v1', masterRoute)
|
||||
|
||||
//Import routes
|
||||
const auth = require('./routes/authRouter.js')
|
||||
masterRoute.use('/auth', auth)
|
||||
|
||||
masterRoute.get('/', (req, res) => {
|
||||
res.send('Welcome to the Telegram to Notion Bot Backend!')
|
||||
})
|
||||
|
||||
masterRoute.get('/privacy-policy', (req, res) => {
|
||||
res.render('privacy-policy')
|
||||
})
|
||||
|
||||
masterRoute.get('/terms-of-use', (req, res) => {
|
||||
res.render('terms-of-use')
|
||||
})
|
||||
|
||||
app.listen(port, () => console.log('port', port))
|
||||
60
src/routes/authRouter.js
Normal file
60
src/routes/authRouter.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const Express = require('express');
|
||||
const router = Express.Router()
|
||||
|
||||
const axios = require('axios')
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
|
||||
async function requestAccessToken() {
|
||||
try {
|
||||
const reqData = {
|
||||
code: req.query.code,
|
||||
grant_type: "authorization_code",
|
||||
redirect_uri: "https://telegram-to-notion.herokuapp.com/auth"
|
||||
}
|
||||
|
||||
const auth = {
|
||||
Authorization: "Basic " + Buffer.from(`${process.env.NOTION_INTEGRATION_ID}:${process.env.NOTION_INTEGRATION_SECRET}`).toString('base64')
|
||||
}
|
||||
|
||||
console.log(auth)
|
||||
|
||||
const response = await axios({
|
||||
method: "POST",
|
||||
url: "https://api.notion.com/v1/oauth/token",
|
||||
data: reqData,
|
||||
auth: {
|
||||
username: Buffer.from(process.env.NOTION_INTEGRATION_ID.toString('base64')),
|
||||
password: Buffer.from(process.env.NOTION_INTEGRATION_SECRET.toString('base64'))
|
||||
} //THANK YOU https://stackoverflow.com/questions/67534080/notion-api-invalid-client-oauth-integration/68699544#68699544?newreg=949504cf865c4a52b2c0ce7afe936c9b
|
||||
})
|
||||
|
||||
console.log(response.status) //400 in positive case
|
||||
console.log(response.data)
|
||||
|
||||
/**
|
||||
* access_token: string,
|
||||
* token_type: string,
|
||||
* bot_id: string,
|
||||
* workspace_name: string,
|
||||
* workspace_icon: string,
|
||||
* workspace_id: string
|
||||
*/
|
||||
|
||||
return response
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error)
|
||||
|
||||
return {status: 400}
|
||||
}
|
||||
}
|
||||
|
||||
const response = await requestAccessToken()
|
||||
|
||||
res.status(response.status).json(
|
||||
response
|
||||
)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user