flyingflip / oauth2-fitbit
PHP League OAuth2-Client 的 Fitbit OAuth 2.0 客户端提供程序
1.2.4
2023-08-13 11:41 UTC
Requires
- php: >=5.6.0
- league/oauth2-client: ^2.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ~1.0
- jakub-onderka/php-parallel-lint: ~0.9
- mockery/mockery: 0.9.*
- phpunit/phpunit: ^5.0
- squizlabs/php_codesniffer: ^2.0
This package is auto-updated.
Last update: 2024-09-13 13:56:18 UTC
README
本程序包为 PHP League 的 OAuth 2.0 客户端 提供Fitbit OAuth 2.0 支持。
本程序包符合 PSR-1、PSR-2、PSR-4 和 PSR-7 标准。如果您发现符合标准的疏漏,请通过拉取请求发送补丁。
开发者可以在 https://dev.fitbit.com/apps 上注册应用程序以使用 Fitbit API。
要求
以下版本的 PHP 都受支持。
- PHP 5.6
- PHP 7.0
- PHP 7.1
- HHVM
安装
要安装,请使用 composer
composer require djchen/oauth2-fitbit
用法
授权码授予
use djchen\OAuth2\Client\Provider\Fitbit; $provider = new 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']) || array_key_exists('oauth2state', $_SESSION) && ($_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( Fitbit::METHOD_GET, Fitbit::BASE_FITBIT_API_URL . '/1/user/-/profile.json', $accessToken, ['headers' => [Fitbit::HEADER_ACCEPT_LANG => 'en_US'], [Fitbit::HEADER_ACCEPT_LOCALE => 'en_US']] // Fitbit uses the Accept-Language for setting the unit system used // and setting Accept-Locale will return a translated response if available. // https://dev.fitbit.com/docs/basics/#localization ); // Make the authenticated API request and get the parsed response. $response = $provider->getParsedResponse($request); // If you would like to get the response headers in addition to the response body, use: //$response = $provider->getResponse($request); //$headers = $response->getHeaders(); //$parsedResponse = $provider->parseResponse($response); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token or user details. exit($e->getMessage()); } }
刷新令牌
一旦您的应用程序获得授权,您可以使用刷新令牌来刷新已过期的令牌,而不是通过整个获取全新令牌的过程。为此,只需从您的数据存储中重复使用此刷新令牌来请求刷新。
$provider = new djchen\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. }
测试
$ ./vendor/bin/phpunit
贡献
请参阅 CONTRIBUTING 了解详情。
许可
MIT 许可证 (MIT)。请参阅 许可文件 获取更多信息。