From f9b9163f5f0f7821531df0214c31e1c01dc29574 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 22 Aug 2019 12:52:38 +0200 Subject: [PATCH] meetup OAuth2 --- package.json | 2 ++ webtask-meetup.js | 64 +++++++++++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 03bc392..0fb4b48 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,10 @@ "test": "eslint ./*.js" }, "dependencies": { + "axios": "^0.19.0", "body-parser": "^1.19.0", "cors": "^2.8.5", + "dotenv": "^8.1.0", "express": "^4.17.1", "ms": "^2.1.2", "request": "^2.88.0", diff --git a/webtask-meetup.js b/webtask-meetup.js index 3323ca8..c79522e 100644 --- a/webtask-meetup.js +++ b/webtask-meetup.js @@ -3,9 +3,12 @@ const Webtask = require('webtask-tools') const cors = require('cors') const bodyParser = require('body-parser') const request = require('request') +const axios = require('axios') const server = express() +require('dotenv').config() + server.listen(4430) server.use(bodyParser.json()) @@ -21,35 +24,54 @@ server.use(cors(corsOptions)) const baseUrl = 'https://api.meetup.com' -server.get('/', async (req, res) => { +const oAuthFlow = async () => { // // OAuth2 Authentication // - const { MEETUP_OAUTH_KEY, MEETUP_OAUTH_SECRET } = req.webtaskContext.secrets + // const { MEETUP_OAUTH_KEY, MEETUP_OAUTH_SECRET } = req.webtaskContext.secrets + const { + MEETUP_OAUTH_KEY, + MEETUP_OAUTH_SECRET, + MEETUP_EMAIL, + MEETUP_PASSWORD + } = process.env // Requesting Authorization const authOptions = { - url: `https://secure.meetup.com/oauth2/authorize?client_id=${MEETUP_OAUTH_KEY}&redirect_uri=https://oceanprotocol.com&response_type=anonymous_code` + url: `https://secure.meetup.com/oauth2/authorize?client_id=${MEETUP_OAUTH_KEY}&redirect_uri=/&response_type=anonymous_code`, + headers: { + Accept: 'application/json' + } } - - const code = await request.get(authOptions, (error, response, body) => { - if (error) res.send(error) - console.log(body) - return JSON.parse(body).code - }) + const response = await axios(authOptions) + console.log(response.data) // Requesting Access Token - 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}` - } - const token = request.post(tokenAuthOptions, (error, response, body) => { - if (error) res.send(error) - console.log(body) - return JSON.parse(body).access_token - }) + // 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() const options = { - url: `${baseUrl}/pro/data-economy/groups?access_token=${token}` + url: `${baseUrl}/pro/data-economy/groups?access_token=${oauthToken}` } try { @@ -57,7 +79,7 @@ server.get('/', async (req, res) => { const data = JSON.parse(body) let members = [] - if (error) res.send(error) + if (error) res.send(error.error_description) if (response.statusCode !== 200) res.send(body) if (Array.isArray(data)) { @@ -71,8 +93,8 @@ server.get('/', async (req, res) => { res.send({ groups: data, members }) }) } catch (error) { - console.error(error) - res.send(error) + console.error(error.error_description) + res.send(error.error_description) } })