jmitchell38488 / oauth2-fitbit
使用thephpleague OAuth 2.0客户端的FitBit API的OAuth 2.0包装器
v0.1.3
2015-11-12 00:50 UTC
Requires
- php: >=5.5.0
- league/oauth2-client: ^1.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-18 18:58:46 UTC
README
此包使您轻松地将应用程序与FitBit OAuth 2.0服务提供商集成。
安装
composer require jmitchell38488/oauth2-fitbit
用法
FitBit提供了两种与OAuth 2.0服务进行身份验证的不同方法,一种授权代码授予流程和一种隐式授予流程。两者在实例化提供者时需要不同的配置,而隐式授予流程将少一个步骤。
FitBit还使用了一个不同于父库提供的不同授权头。当用户使用FitBit 2.0 API进行身份验证时,他们需要将Authorization: Basic设置为生成访问令牌,并在后续请求中提供授权头,但使用Bearer而不是Basic。
该包包括三个具体的提供者类和一个抽象提供者类。抽象提供者类为授权和隐式实现提供了共享功能。FitBit类扩展了Authorization类,因此如果您喜欢,可以使用它代替Authorization类。它在进行身份验证请求时提供了清晰的表示。无论如何,如果您支持隐式或授权代码授予流程,您需要跟踪您用于身份验证会话的是哪一个,因为一个将超时并且您可以刷新它,而另一个在超时后需要用户重新授权。
授权代码授予流程
身份验证会话
session_start(); use Jmitchell38488\OAuth2\Client\Provider\FitBitAuthorization; require_once __DIR__ . '/vendor/autoload.php'; $provider = new FitBitAuthorization([ 'clientId' => $my_client_id_from_fitbit, 'clientSecret' => $my_client_secret_from_fitbit, 'redirectUri' => $my_callback_url, ]); // 1st step: Has the user authorised yet? if (!isset($_SESSION['oauth2state'])) { $authorizationUrl = $provider->getAuthorizationUrl([ 'prompt' => FitBitAuthorization::PROMPT_CONSENT, 'response_type' => FitBitAuthorization::RESPONSETYPE_CODE, 'scope' => $provider->getAllScope(), ]); // Set the session state to validate in the callback $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authorizationUrl); exit; // 2nd step: User has authorised, now lets get the refresh & access tokens } else if (isset($_GET['state']) && $_GET['state'] == $_SESSION['oauth2state'] && isset($_GET['code']) && !isset($_SESSION['fitbit']['oauth'])) { try { $token = base64_encode(sprintf('%s:%s', $my_client_id_from_fitbit, $my_client_secret_from_fitbit)); $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'], 'access_token' => $_GET['code'], 'token' => $token, ]); unset($_SESSION['oauth2state']); $_SESSION['fitbit']['oauth2'] = array( 'accessToken' => $accessToken->getToken(), 'expires' => $accessToken->getExpires(), 'refreshToken' => $accessToken->getRefreshToken(), ); } catch (Exception $ex) { print $ex->getMessage(); } // 3rd step: Authorised, have tokens, but session needs to be refreshed } else if (time() > $_SESSION['fitbit']['oauth2']['expires']) { try { $token = base64_encode(sprintf('%s:%s', $my_client_id_from_fitbit, $my_client_secret_from_fitbit)); $accessToken = $provider->getAccessToken('refresh_token', [ 'grant_type' => FitBitAuthorization::GRANTTYPE_REFRESH, 'access_token' => $_SESSION['fitbit']['oauth2']['accessToken'], 'refresh_token' => $_SESSION['fitbit']['oauth2']['refreshToken'], 'token' => $token, ]); unset($_SESSION['oauth2state']); $_SESSION['fitbit']['oauth2'] = array( 'accessToken' => $accessToken->getToken(), 'expires' => $accessToken->getExpires(), 'refreshToken' => $accessToken->getRefreshToken(), ); } catch (Exception $ex) { print $ex->getMessage(); } }
隐式授予流程
身份验证会话
session_start(); use Jmitchell38488\OAuth2\Client\Provider\FitBitImplicit; require_once __DIR__ . '/vendor/autoload.php'; $provider = new FitBitImplicit([ 'clientId' => $my_client_id_from_fitbit, 'clientSecret' => $my_client_secret_from_fitbit, 'redirectUri' => $my_callback_url, ]); // 1st step: Has the user authorised yet? Or do we need to refresh? if (!isset($_SESSION['oauth2state'])) { $authorizationUrl = $provider->getAuthorizationUrl([ 'prompt' => FitBitImplicit::PROMPT_CONSENT, 'response_type' => FitBitImplicit::RESPONSETYPE_TOKEN, 'scope' => $provider->getAllScope(), 'expires_in' => FitBitImplicit::EXPIRES_IN_DAY // This can be set to 1, 7 or 30 days ]); // Set the session state to validate in the callback $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authorizationUrl); exit; // 2nd step: User has authorised, now lets get the refresh & access tokens // The return URL uses fragments, so you will need to implement front-end logic to redirect the // user back to the server with the relevant information, since the URL will look like: // my_callback_uri#scope=nutrition+weight+location+social+heartrate+settings+sleep+activity+profile&state=abcdef1234567890&user_id=ABC123&token_type=Bearer&expires_in=86400&access_token=abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890 } else if (isset($_GET['state']) && $_GET['state'] == $_SESSION['oauth2state'] && isset($_GET['access_token']) && !isset($_SESSION['fitbit']['oauth'])) { unset($_SESSION['oauth2state']); $_SESSION['fitbit']['oauth2'] = array( 'accessToken' => $_GET['access_token'], 'expires' => $_GET['expires_in'], 'refreshToken' => null, ); }
发出请求
使用FitBit类而不是授权代码授予流程类非常重要,因为FitBit API要求您在授权头中使用Bearer令牌,而不是Basic令牌。如果您不使用FitBit类,API将返回401未授权错误。
要发出请求
$endpoint = $provider->getBaseApiUrl() . "user/-/profile." . FitBit::FORMAT_JSON; $provider = new FitBit([ 'clientId' => $my_client_id_from_fitbit, 'clientSecret' => $my_client_secret_from_fitbit, 'redirectUri' => $my_callback_url, ]); $request = $provider->getAuthenticatedRequest( FitBit::METHOD_GET, $endpoint, $_SESSION['fitbit']['oauth2']['accessToken'] ); $response = $provider->getResponse($request);