1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2025-01-08 13:02:53 +01:00

meetup OAuth2

This commit is contained in:
Matthias Kretschmann 2019-08-22 12:52:38 +02:00
parent d428605f5c
commit f9b9163f5f
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 45 additions and 21 deletions

View File

@ -7,8 +7,10 @@
"test": "eslint ./*.js" "test": "eslint ./*.js"
}, },
"dependencies": { "dependencies": {
"axios": "^0.19.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^8.1.0",
"express": "^4.17.1", "express": "^4.17.1",
"ms": "^2.1.2", "ms": "^2.1.2",
"request": "^2.88.0", "request": "^2.88.0",

View File

@ -3,9 +3,12 @@ const Webtask = require('webtask-tools')
const cors = require('cors') const cors = require('cors')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const request = require('request') const request = require('request')
const axios = require('axios')
const server = express() const server = express()
require('dotenv').config()
server.listen(4430) server.listen(4430)
server.use(bodyParser.json()) server.use(bodyParser.json())
@ -21,35 +24,54 @@ server.use(cors(corsOptions))
const baseUrl = 'https://api.meetup.com' const baseUrl = 'https://api.meetup.com'
server.get('/', async (req, res) => { const oAuthFlow = async () => {
// //
// OAuth2 Authentication // 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 // Requesting Authorization
const authOptions = { 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 response = await axios(authOptions)
const code = await request.get(authOptions, (error, response, body) => { console.log(response.data)
if (error) res.send(error)
console.log(body)
return JSON.parse(body).code
})
// Requesting Access Token // Requesting Access Token
const tokenAuthOptions = { // 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}` // 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 token = request.post(tokenAuthOptions, (error, response, body) => { // }
if (error) res.send(error) // const accessToken = await axios(tokenAuthOptions).access_token
console.log(body)
return JSON.parse(body).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 = { const options = {
url: `${baseUrl}/pro/data-economy/groups?access_token=${token}` url: `${baseUrl}/pro/data-economy/groups?access_token=${oauthToken}`
} }
try { try {
@ -57,7 +79,7 @@ server.get('/', async (req, res) => {
const data = JSON.parse(body) const data = JSON.parse(body)
let members = [] let members = []
if (error) res.send(error) if (error) res.send(error.error_description)
if (response.statusCode !== 200) res.send(body) if (response.statusCode !== 200) res.send(body)
if (Array.isArray(data)) { if (Array.isArray(data)) {
@ -71,8 +93,8 @@ server.get('/', async (req, res) => {
res.send({ groups: data, members }) res.send({ groups: data, members })
}) })
} catch (error) { } catch (error) {
console.error(error) console.error(error.error_description)
res.send(error) res.send(error.error_description)
} }
}) })