waytohealth / oauth2-withings
Withings OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Installs: 12 451
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 8
Open Issues: 2
Requires
- php: >=5.6.0
- league/oauth2-client: 2.4.*
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.19
- jakub-onderka/php-parallel-lint: ~1.0
- mockery/mockery: ~1.3
- phpstan/phpstan: ^0.12.89
- phpunit/phpunit: ^5.0
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-11-29 04:52:33 UTC
README
This package provides Withings OAuth 2.0 support for the PHP League's OAuth 2.0 Client.
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.
Requirements
The following versions of PHP are supported.
- PHP 5.6
- PHP 7.0
- PHP 7.1
- HHVM
Installation
To install, use composer:
composer require waytohealth/oauth2-withings
Usage
Authorization Code Grant
use waytohealth\OAuth2\Client\Provider\Withings; $provider = new Withings([ 'clientId' => '{withings-oauth2-client-id}', 'clientSecret' => '{withings-client-secret}', 'redirectUri' => 'https://example.com/callback-url' ]); // 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($options); // Try to get an access token using the authorization code grant. $accessToken = $provider->getAccessToken('authorization_code', $options); // Add subscription $subscriptionUrl = sprintf('https://wbsapi.withings.net/notify?action=subscribe&access_token=%s&callbackurl=%s&appli=%s&comment=Way_To_Health', $accessToken, $params['callbackurl'], $params['appli'] ); $subscriptionRequest = $provider->getAuthenticatedRequest('GET', $subscriptionUrl, $accessToken, $options); $provider->getParsedResponse($request); // Get data $request = $provider->getAuthenticatedRequest('GET', $url, $accessToken, $options); $data = $provider->getParsedResponse($request);
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.
$provider = new waytohealth\OAuth2\Client\Provider\Withings([ 'clientId' => '{withings-oauth2-client-id}', 'clientSecret' => '{withings-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
$ ./vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.