Authorization & token (log in to API) - PJanisio/ewelinkApiPhp GitHub Wiki

Gateway

This class is using gateway method to authorize. It is a common way to log in to APIs -> get confirmation (as a token) and than you can use methods and function to operate the devices. That is why API needs your REDIRECT_URL.

In our case, if you copy and paste directly from github - the gateway is index.php. But our index.php has almost all methods written there, so lets put an example of gateway only:


<?php
//load all classes
require_once __DIR__ . '/autoloader.php';

//Class init
$http = new HttpClient();

//Get token
$token = $http->getToken();

    if ($token->checkAndRefreshToken()) {
        echo 'I`m authorized successfully';
    } else {

        //if we have no token, or we are not authorized paste link to authorization
        $loginUrl = $http->getLoginUrl();
        echo '<a href="' . htmlspecialchars($loginUrl) . '">Authorize ewelinkApiPhp</a>';
    }    
    ?>

If you configuration data in Constants.php is right, you should see only link to log in to ewelink API:

Authorize ewelinkApiPhp

When you successfully authorized using your email and password, you will see Your TOKEN credentials:


Token Data
Array
(
    [accessToken] => cce4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    [atExpiredTime] => 1721581445941
    [refreshToken] => 3caxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    [rtExpiredTime] => 1724173445962
)
You will be redirected in 1 second...

And token.json will be saved in your project folder to save token data for future use.

Afterwards you will be redirected to your REDIRECT_URL you have specyfied and you should see:

Token is valid
Token expiry time: 2024-07-21 19:04:05

If you encounter any errors and you can not resolve them yourself - place an Issue.

Next step:

Getting devices and deviceId