Code examples
Example codes for the API
'use strict';
const simpleOauthModule = require('simple-oauth2');
const app = require('express')();
const port = 3000;
const createApplication = (cb) => {
const callbackUrl = 'http://localhost:3000/callback';
app.listen(port, (err) => {
if (err) return console.error(err);
console.log(`Express server listening at http://localhost:${port}`);
cb({app, callbackUrl});
});
};
createApplication(({ app, callbackUrl }) => {
const oauth2 = simpleOauthModule.create({
client: {
id: 'INSERT CLIENT_ID HERE',
secret: 'INSERT CLIENT_SECRET HERE',
},
auth: {
tokenHost: 'https://accounts.airthings.com',
tokenPath: 'https://accounts-api.airthings.com/v1/token',
authorizePath: '/authorize',
},
options: {
authorizationMethod: 'body',
}
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: callbackUrl,
scope: 'read:device',
});
// Initial page redirecting to Airthings
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: callbackUrl,
};
try {
const result = await oauth2.authorizationCode.getToken(options);
const tokens = oauth2.accessToken.create(result);
return res.status(200).json(tokens);
} catch(error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});
app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Airthings</a>');
});
});
'use strict';
const simpleOauthModule = require('simple-oauth2');
const Wreck = require('@hapi/wreck');
const app = require('express')();
const port = 3000;
const createApplication = (cb) => {
const callbackUrl = 'http://localhost:3000/callback';
app.listen(port, (err) => {
if (err) return console.error(err);
console.log(`Express server listening at http://localhost:${port}`);
cb({
app,
callbackUrl,
});
});
};
const getDevices = async function (token) {
const options = {
headers: {'Authorization': token.access_token}
};
const { res, payload } = await Wreck.get('https://ext-api.airthings.com/v1/devices', options);
return JSON.parse(payload.toString());
};
createApplication(({ app, callbackUrl }) => {
const oauth2 = simpleOauthModule.create({
client: {
id: 'INSERT CLIENT_ID HERE',
secret: 'INSERT CLIENT_SECRET HERE',
},
auth: {
tokenHost: 'https://accounts.airthings.com',
tokenPath: 'https://accounts-api.airthings.com/v1/token',
authorizePath: '/authorize',
},
options: {
authorizationMethod: 'body',
}
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: callbackUrl,
scope: 'read:device',
});
// Initial page redirecting to Airthings
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: callbackUrl,
};
try {
const result = await oauth2.authorizationCode.getToken(options);
const tokens = oauth2.accessToken.create(result);
const devices = await getDevices(tokens.token);
return res.status(200).json(devices);
} catch(error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});
app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Airthings</a>');
});
});
Updated 4 months ago