brulath / fitbit-php-oauth2
基于 djchen/oauth2-fitbit & heyitspavel/fitbitphp 的 FitBit OAuth 2.0 客户端库
Requires
- league/oauth2-client: ~1.2
- psr/log: ^1.0
- sabre/event: ^3.0
This package is not auto-updated.
Last update: 2021-08-07 01:39:43 UTC
README
使用 OAuth2 的 PHP 不官方 FitBit 客户端库
大量借鉴自 djchen/OAuth2-Fitbit(对错误检查和作用域处理做了轻微修改)和 pavelrisenberg/fitbitphp。
在认证流程中设置 fitbit-php-oauth2-state cookie 以防止 CSRF 攻击。在此之前必须先开始一个会话。
不一定在任何情况下都能正常工作,但有时它还是很有用的。
安装
要安装,请使用 composer
composer require brulath/fitbit-php-oauth2
使用
初始化
以下所有示例都将假设有一个 $fitbit 可用。它目前是状态性的,因此在使用它之前必须设置正确的令牌。
$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);
令牌刷新警告
OAuth2 令牌会过期;有时它们会很快过期。为了避免手动从 Fitbit 获取更新的令牌,当库检测到您尝试对一个过期的令牌执行操作时,它会为您完成此操作。好消息是这意味着您可以不那么关注令牌的刷新,但坏消息是您需要警惕并在令牌更改时获取更新的令牌。
有两种方法来捕获令牌获取
订阅令牌更改事件
$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."); });
检查 POST 请求中的令牌更改
如果您出于某种原因不希望使用事件,您可以在每次调用 API 后检查令牌
$token = $fitbit->getToken(); if ($old_token != $token) { print("Acquired token {$token}."); }
因为我比较懒,所以我在调用过程中自动刷新过期的 OAuth 详细信息。这意味着在每次调用之后,OAuth 令牌可能会更改,您需要检查(并保存新令牌)。我认为检查更改的令牌可能比捕获令牌过期异常并处理它们更容易。抱歉了,兄弟。
您有两个选择:在每次调用后检查 $fitbit->getToken()
,或者订阅 事件。
$fitbit->on('obtain-token', <your_token_saving_function>); $fitbit->on('refresh-token', <your_token_saving_function>);
日志记录
如果您想跟踪自动事件进行调试,请获取 MonoLog(或其他)并在初始化期间传递一个实例作为 'logger'。
composer require monolog/monolog
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. ]);
授权
授权获取Fitbit账户的OAuth2密钥。您必须从Fitbit开发者网站获取有效的客户端ID、客户端密钥和重定向URI才能使用此库。您必须在此处指定您希望使用的所有权限;如果您以后想扩展权限,您需要重新授权用户。
自动授权流程
如果您比较懒(嗨!),可以让库帮您将用户重定向到Fitbit网站。
// A session is required to prevent CSRF session_start(); $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}";
手动授权流程
如果您比较懒(嗨!),可以让库帮您将用户重定向到Fitbit网站。
授权涉及将用户发送到Fitbit网站,并附带一个'状态'代码,这样我们就可以验证请求是否来自我们。存储状态并将用户发送到URI。
$auth = $fitbit->getAuthUrlAndState(); saveStateSoWeCanCheckItLater($auth['state']); // $_SESSION['fitbit-php-oauth2-state'] = $auth['state'] redirectUserToFitbit($auth['uri']);
当用户返回到Fitbit开发者网站上指定的重定向URI时,将会设置一个查询($_GET)参数,其中包含我们之前存储的状态;检查它们是否匹配,以确保请求来自我们。
$state = retrieveQueryString('state'); // $_GET['state'] $storedState = retrieveStoredState(); // $_SESSION['fitbit-php-oauth2-state'] if ($state != $storedState) { throw \Exception("Invalid auth request"); } $code = retrieveQueryString('code'); // $_GET['code'] $fitbit->handleAuthResponse($code); // emits obtain-token event $token = $this->getToken(); echo "My Fitbit access token is: {$token}";
恢复访问
// If token has expired, the first request you make will additionally make a refresh request $fitbit->setToken(getAccessTokenJsonAsArrayFromMyDatabase());
发送请求
检查FitbitPHPOAuth2类以找到适当的方法。在这种情况下,我想要某个日期上的所有活动
$activities = $fitbit->getActivities('2016-02-20'); print_r($activities);
许可
MIT许可(MIT)。