1. Go to this page and download the library: Download brulath/fitbit-php-oauth2 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/ */
brulath / fitbit-php-oauth2 example snippets
$fitbit = new brulath\fitbit\FitbitPHPOAuth2([
'client_id' => 'your_client_id',
'client_secret' => 'your_client_secret',
'redirect_uri' => 'https://www.example.com/fitbit/auth', // must match URI specified in your app on the Fitbit Developer website
'logger' => $log,
'auto_request' => true, // automatically redirect the user to the Fitbit OAuth process if a token doesn't exist
'auto_refresh' => true, // automatically refresh expired tokens
]);
$json_encoded_oauth2_token_for_user = getOAuth2TokenForUserFromMyDatabase();
$fitbit->setToken($json_encoded_oauth2_token_for_user);
$profile = $fitbit->getProfile(); // read warning below about token refreshes
print_r($profile);
$fitbit->on('obtain-token', function( [ $token ] ) {
print("Acquired first token {$token} for the user; I'll save this to the database.");
});
$fitbit->on('refresh-token', function( [ $token ] ) {
print("Acquired refresh token {$token} so I should update the database with this user's new OAuth2 token.");
});
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
$fitbit = new brulath\fitbit\FitbitPHPOAuth2([
'logger' => $log,
// etc.
]);
// A session is ;
$json_encoded_oauth2_token_for_user = $fitbit->getToken(); // will redirect user to fitbit ($fitbit->doAuthFlow()). the cookie it sets must survive.
echo "My Fitbit access token is: {$json_encoded_oauth2_token_for_user}";
// If token has expired, the first request you make will additionally make a refresh request
$fitbit->setToken(getAccessTokenJsonAsArrayFromMyDatabase());