2022-07-11 05:41:50 -07:00
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
async function getToken(code) {
|
|
|
|
const params = {
|
|
|
|
client_id: process.env['WITHINGS_CLIENT_ID'],
|
|
|
|
client_secret: process.env['WITHINGS_CLIENT_SECRET'],
|
|
|
|
code: code,
|
|
|
|
grant_type: 'authorization_code',
|
|
|
|
action: 'requesttoken',
|
2022-07-12 11:44:10 -07:00
|
|
|
redirect_uri: 'https://ohmyposh.dev/api/auth',
|
2022-07-11 05:41:50 -07:00
|
|
|
};
|
2022-07-12 11:44:10 -07:00
|
|
|
|
2022-07-11 05:41:50 -07:00
|
|
|
const resp = await axios.post('https://wbsapi.withings.net/v2/oauth2', null, { params: params });
|
|
|
|
|
2022-07-12 11:44:10 -07:00
|
|
|
if (resp.data.error) {
|
|
|
|
throw resp.data.error;
|
|
|
|
}
|
|
|
|
|
2022-07-11 05:41:50 -07:00
|
|
|
return {
|
2022-07-12 11:44:10 -07:00
|
|
|
access_token: resp.data.body.access_token,
|
|
|
|
refresh_token: resp.data.body.refresh_token,
|
|
|
|
expires_in: resp.data.body.expires_in
|
2022-07-11 05:41:50 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function refreshToken(refresh_token) {
|
|
|
|
const params = {
|
|
|
|
client_id: process.env['WITHINGS_CLIENT_ID'],
|
|
|
|
client_secret: process.env['WITHINGS_CLIENT_SECRET'],
|
|
|
|
refresh_token: refresh_token,
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
action: 'requesttoken',
|
2022-07-12 11:44:10 -07:00
|
|
|
redirect_uri: 'https://ohmyposh.dev/api/auth',
|
2022-07-11 05:41:50 -07:00
|
|
|
};
|
|
|
|
const resp = await axios.post('https://wbsapi.withings.net/v2/oauth2', null, { params: params });
|
|
|
|
|
|
|
|
return {
|
2022-07-12 11:44:10 -07:00
|
|
|
access_token: resp.data.body.access_token,
|
|
|
|
refresh_token: resp.data.body.refresh_token,
|
|
|
|
expires_in: resp.data.body.expires_in
|
2022-07-11 05:41:50 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getToken: getToken,
|
|
|
|
refreshToken: refreshToken,
|
|
|
|
}
|