2017 © Pedro Peláez
 

library oauth2-fitbit

Fitbit OAuth 2.0 Client Provider for The PHP League OAuth2-Client

image

hwd/oauth2-fitbit

Fitbit OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  • Saturday, February 6, 2016
  • by bradmkjr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Fitbit Provider for OAuth 2.0 Client

This is a fork of https://github.com/djchen/oauth2-fitbit for the purposes of having a non dev version available for composer install., (*1)

https://packagist.org/packages/hwd/oauth2-fitbit, (*2)

This package provides Fitbit OAuth 2.0 support for the PHP League's OAuth 2.0 Client., (*3)

This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-7. If you notice compliance oversights, please send a patch via pull request., (*4)

Requirements

The following versions of PHP are supported., (*5)

  • PHP 5.5
  • PHP 5.6
  • PHP 7.0
  • HHVM

Installation

To install, use composer:, (*6)

composer require hwd/oauth2-fitbit

Usage

Authorization Code Grant

$provider = new hwd\OAuth2\Client\Provider\Fitbit([
    'clientId'          => '{fitbit-oauth2-client-id}',
    'clientSecret'      => '{fitbit-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

// start the session
session_start();

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();

    // Get the state generated for you and store it to the session.
    $_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() . "\n";
        echo $accessToken->getRefreshToken() . "\n";
        echo $accessToken->getExpires() . "\n";
        echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n";

        // Using the access token, we may look up details about the
        // resource owner.
        $resourceOwner = $provider->getResourceOwner($accessToken);

        var_export($resourceOwner->toArray());

        // 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.fitbit.com/1/user/-/profile.json',
            $accessToken
        );
        // Make the authenticated API request and get the response.
        //$response = $provider->getResponse($request);

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

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

    }

}

Refreshing a Token

Once your application is authorized, you can refresh an expired token using a refresh token rather than going through the entire process of obtaining a brand new token. To do so, simply reuse this refresh token from your data store to request a refresh., (*7)

$provider = new hwd\OAuth2\Client\Provider\Fitbit([
    'clientId'          => '{fitbit-oauth2-client-id}',
    'clientSecret'      => '{fitbit-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

$existingAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $existingAccessToken->getRefreshToken()
    ]);

    // Purge old access token and store new access token to your data store.
}

Testing

bash $ ./vendor/bin/phpunit, (*8)

Contributing

Please see CONTRIBUTING for details., (*9)

License

The MIT License (MIT). Please see License File for more information., (*10)

The Versions

06/02 2016

dev-master

9999999-dev

Fitbit OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization oauth client oauth2 fitbit

06/02 2016

1.0.1

1.0.1.0

Fitbit OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization oauth client oauth2 fitbit

06/02 2016

1.0.0

1.0.0.0

Fitbit OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization oauth client oauth2 fitbit

17/11 2015

0.1.0

0.1.0.0

Fitbit OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication authorization oauth client oauth2 fitbit