First steps
Published:
OAuth2
The everHome API uses OAuth2 for authentication.
First, create an OAuth2 application under My Applications.
Keep the Client ID and Client Secret safe.
HTTP
All HTTP requests sent to the everHome API must be authorized using the token generated by OAuth2.
The current token must be included in the HTTP header for this purpose.
GET https://everhome.cloud/device HTTP/1.1
Authorization: Bearer $TOKEN
Body parameters are passed using JSON.
POST https://everhome.cloud/device/56/execute HTTP/1.1
Authorization: Bearer $TOKEN
Content-Type: application/json
\n
{
"action": "on"
}
OAuth2 Login Example
export CLIENT_ID="Your Client ID"
export CLIENT_SECRET="Your Client Secret"
const app = require('express')();
const { AuthorizationCode } = require('simple-oauth2');
const port = 3000;
const createApplication = (cb) => {
const callbackUrl = 'http://localhost:3000/callback';
app.listen(port, (err) => {
if (err) return console.error(err);
console.log(`http://localhost:${port}`);
return cb({
app,
callbackUrl,
});
});
};
createApplication(({ app, callbackUrl }) => {
const client = new AuthorizationCode({
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
},
auth: {
tokenHost: 'https://everhome.cloud',
authorizeHost: 'https://everhome.cloud',
tokenPath: '/oauth2/token',
authorizePath: '/oauth2/authorize',
},
http: {
json: 'force',
headers: {
accept: "text/html"
}
},
options: {
authorizationMethod: 'body',
},
});
const authorizationUri = client.authorizeURL({
redirect_uri: callbackUrl,
state: '3',
});
// Calls the everHome OAuth2 page
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Captures the response from the OAuth2 page
app.get('/callback', async (req, res) => {
const { code } = req.query;
const options = {
code,
redirect_uri: callbackUrl,
};
try {
const accessToken = await client.getToken(options);
console.log('Your everHome token: ', accessToken.token);
persistToken(accessToken.token); // save token
return res.status(200).json(accessToken.token);
} catch (error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});
app.get('/', (req, res) => {
res.send('<a href="/auth">Login</a>');
});
});
Renewing OAuth2 Token
The received OAuth2 token expires after a certain time for security reasons.
Therefore, check before each HTTP request whether the token to be used is still valid.
const accessTokenJSONString = loadTokenFromStorage();
let accessToken = client.createToken(JSON.parse(accessTokenJSONString));
if (accessToken.expired(30)) {
try {
accessToken = await accessToken.refresh();
} catch (error) {
console.log('Error refreshing access token: ', error.message);
}
}