1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2025-01-08 21:04:03 +01:00
webtasks/webtask-meetup.js

102 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2019-08-07 12:25:42 +02:00
const express = require('express')
const Webtask = require('webtask-tools')
const cors = require('cors')
const bodyParser = require('body-parser')
const request = require('request')
2019-08-22 12:52:38 +02:00
const axios = require('axios')
2019-08-07 12:25:42 +02:00
const server = express()
2019-08-22 12:52:38 +02:00
require('dotenv').config()
2019-08-07 12:25:42 +02:00
server.listen(4430)
server.use(bodyParser.json())
//
// Allow requests from these domains only
//
const corsOptions = {
origin: ['https://oceanprotocol.com', /\.oceanprotocol\.com$/],
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
server.use(cors(corsOptions))
const baseUrl = 'https://api.meetup.com'
2019-08-22 12:52:38 +02:00
const oAuthFlow = async () => {
2019-08-21 12:53:29 +02:00
//
// OAuth2 Authentication
//
2019-08-22 12:52:38 +02:00
// const { MEETUP_OAUTH_KEY, MEETUP_OAUTH_SECRET } = req.webtaskContext.secrets
const {
MEETUP_OAUTH_KEY,
MEETUP_OAUTH_SECRET,
MEETUP_EMAIL,
MEETUP_PASSWORD
} = process.env
2019-08-21 12:53:29 +02:00
// Requesting Authorization
const authOptions = {
2019-08-22 12:52:38 +02:00
url: `https://secure.meetup.com/oauth2/authorize?client_id=${MEETUP_OAUTH_KEY}&redirect_uri=/&response_type=anonymous_code`,
headers: {
Accept: 'application/json'
}
2019-08-21 12:53:29 +02:00
}
2019-08-22 12:52:38 +02:00
const response = await axios(authOptions)
console.log(response.data)
2019-08-21 12:53:29 +02:00
// Requesting Access Token
2019-08-22 12:52:38 +02:00
// const tokenAuthOptions = {
// url: `https://secure.meetup.com/oauth2/access?client_id=${MEETUP_OAUTH_KEY}&client_secret=${MEETUP_OAUTH_SECRET}&grant_type=anonymous_code&redirect_uri=https://oceanprotocol.com&code=${code}`,
// method: 'POST'
// }
// const accessToken = await axios(tokenAuthOptions).access_token
// // Send user credentials
// const userAuthOptions = {
// url: `https://api.meetup.com/sessions?&email=${MEETUP_EMAIL}&password=${MEETUP_PASSWORD}`,
// method: 'POST',
// headers: {
// Authorization: `Bearer ${accessToken}`
// }
// }
// const oauthToken = await await axios(userAuthOptions).oauth_token
// return oauthToken
}
server.get('/', async (req, res) => {
const oauthToken = await oAuthFlow()
2019-08-07 12:25:42 +02:00
const options = {
2019-08-22 12:52:38 +02:00
url: `${baseUrl}/pro/data-economy/groups?access_token=${oauthToken}`
2019-08-07 12:25:42 +02:00
}
try {
request.get(options, (error, response, body) => {
const data = JSON.parse(body)
let members = []
2019-08-22 12:52:38 +02:00
if (error) res.send(error.error_description)
2019-08-21 11:46:02 +02:00
if (response.statusCode !== 200) res.send(body)
if (Array.isArray(data)) {
for (const item of data) {
members.push(item.member_count)
}
members = members.reduce((a, b) => a + b, 0)
2019-08-07 12:25:42 +02:00
}
res.send({ groups: data, members })
})
} catch (error) {
2019-08-22 12:52:38 +02:00
console.error(error.error_description)
res.send(error.error_description)
2019-08-07 12:25:42 +02:00
}
})
module.exports = Webtask.fromExpress(server)