PHP code example of auburus / netatmo-api

1. Go to this page and download the library: Download auburus/netatmo-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

auburus / netatmo-api example snippets



use Auburus\OAuth2\Client\Provider\NetatmoThermostat;

$provider = new NetatmoThermostat([
    'clientId'      => 'XXXXXXXX',
    'clientSecret'  => 'XXXXXXXX',
    'redirectUri'   => 'https://your-registered-redirect-uri/',
]);

// (All the OAuth2 proces...)
// ...


$resourceOwner = $provider->getResourceOwner($accessToken);

var_export($resourceOwner->toArray());

use Auburus\OAuth2\Client\Provider\NetatmoThermostat as Netatmo;
 php


us\OAuth2\Client\Provider\Netatmo;
use GuzzleHttp\Exception\RequestException;

session_start();

$provider = new Netatmo([
    'clientId'      => 'XXXXXXXX',
    'clientSecret'  => 'XXXXXXXX',
    'redirectUri'   => 'https://your-registered-redirect-uri/',
]);

// Handles the case when the user choose to NOT authorize
if (isset($_GET['error'])) {
    echo $_GET['error'];
    exit;
}

if (!isset($_GET['code'])) {


    $authorizationUrl = $provider->getAuthorizationUrl([
        'scope' => ['read_station']
    ]);

    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    try {

        // Try to get an access token using the authorization code grant.
        $accessToken = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);

        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo $accessToken->getToken() . "<br>";
        echo $accessToken->getRefreshToken() . "<br>";
        echo $accessToken->getExpires() . "<br>";
        echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";

        // The provider provides a way to get an authenticated API request for
        // the service, using the access token; it returns an object conforming
        // to Psr\Http\Message\RequestInterface.
        $request = $provider->getAuthenticatedRequest(
            'GET',
            'https://api.netatmo.com/api/getstationsdata?access_token=' . $accessToken,
            $accessToken
        );

        try {
            $response = $provider->getHttpClient()->send($request);
            echo $response->getBody();
        } catch (RequestException $e) {
            echo "<h1>ERROR!</h1>";
            echo $e->getResponse()->getBody();
        }

    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

        // Failed to get the access token or user details.
        exit($e->getMessage());

    }

}