waytohealth/oauth2-withings

为PHP League的OAuth 2.0 Client提供的Withings OAuth 2.0客户端提供者

3.0.0 2021-07-21 16:25 UTC

This package is auto-updated.

Last update: 2024-08-29 04:20:13 UTC


README

CI

此包为PHP League的OAuth 2.0 Client提供Withings OAuth 2.0支持。

此包符合PSR-1PSR-2PSR-4PSR-7规范。如果您发现规范不符合之处,请通过pull request发送补丁。

要求

以下PHP版本得到支持。

  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • HHVM

安装

要安装,请使用composer

composer require waytohealth/oauth2-withings

使用方法

授权码授予

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);

刷新令牌

一旦您的应用程序获得授权,您可以使用刷新令牌来刷新过期的令牌,而不是重新获取全新的令牌。要做到这一点,只需简单地从您的数据存储中重新使用此刷新令牌来请求刷新。

$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.
}

测试

$ ./vendor/bin/phpunit

贡献

请参阅CONTRIBUTING以获取详细信息。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件