andalisolutions/oauth2-anaf

PHP League OAuth 2.0 Client 的 Anaf OAuth 2.0 支持

v1.0.0 2024-01-28 22:05 UTC

This package is auto-updated.

Last update: 2024-08-29 13:13:02 UTC


README

OAuth 2.0 ANAF

GitHub Workflow Status (main) Total Downloads Latest Version License

OAuth 2.0 客户端 ANAF 提供者

本软件包为 PHP League 的 OAuth 2.0 Client 提供了对 ANAF OAuth 2.0 的支持。

安装

安装时使用 composer

composer require andalisolutions/oauth2-anaf

使用方法

使用方法与 The League 的 OAuth 客户端相同,使用 \Anaf\OAuth2\Client\Provider\AnafProvider 作为提供者。

授权码流程

$provider = new Anaf\OAuth2\Client\Provider\AnafProvider(
    clientId: '{anaf-client-id}',
    clientSecret: '{anaf-client-secret}',
    redirectUrl: 'https://example.com/callback-url',
);

// Redirect to the authorization URL
$authorizationUrl = $provider->getAuthorizationUrl();
header('Location: ' . $authorizationUrl);

// This part will be in your callback script
if (isset($_GET['code'])) {
    try {
        $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 'Access Token: ' . $accessToken->getToken() . "<br>";
        echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
        echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
        echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";

    } catch (Exception $e) {
        // Handle errors, such as an invalid code
    }
}

测试

composer test

贡献

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

致谢

许可证

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