vlsv / sber-api-registry-oauth-client
Sber API Registry 的客户端,用于处理令牌
v1.1.9
2022-10-10 11:35 UTC
Requires
- php: ^8.1
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.1
- symfony/serializer-pack: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- overtrue/phplint: ^5.3
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.26
README
Sber API Registry 的客户端,用于处理令牌
获取 access_token 以便后续操作可用的 API。
基本信息
需要在 API 调用端进行的工作改进说明 - API 调用端设置。API 调用根据 OAuth 2.0 规范进行,首先获取一个令牌(access_token),允许直接请求 API。
重要!每次向 API 发起新请求时都需要获取新的令牌。令牌的有效期为 60 秒。
有两种类型的令牌实现
环境变量要求
获取令牌需要
安装库
composer require vlsv/sber-api-registry-oauth-client
获取证书
官方文档
准备证书
openssl pkcs12 -in {path_to_certificate}/cert.p12 -out {path_to_certificate}/cert.pem -nodes
获取 access_token
提供的令牌请求方法参数是最基本的,实际上还有更多(参见源代码)。
require_once(__DIR__ . '/vendor/autoload.php'); $config = new \Vlsv\SberApiRegistryOauthClient\ClientConfig( clientId: 'client_id', clientSecret: 'client_secret', certPath: 'cert_path', certPassword: 'cert_password', ); $oAuthClient = new \Vlsv\SberApiRegistryOauthClient\OAuthClient($config); // OAUTH-токен try { $accessToken = $oAuthClient ->getOauthToken(scope: 'https://api.sberbank.ru/qr/order.create') ->getAccessToken(); } catch (\Vlsv\SberApiRegistryOauthClient\Exception\ApiException $exception) { echo $exception->getMessage(); if ($exception->getResponseObject()) { echo $exception->getResponseObject()->getMoreInformation(); } } catch (\Throwable $exception) { echo $exception->getMessage(); } // OIDC-токен try { $accessToken = $oAuthClient->getOidcToken( scope: 'https://api.sberbank.ru/qr/order.create', code: 'authorization_code', redirectUri: 'redirect_uri', )->getAccessToken(); } catch (\Vlsv\SberApiRegistryOauthClient\Exception\ApiException $exception) { echo $exception->getMessage(); if ($exception->getResponseObject()) { echo $exception->getResponseObject()->getMoreInformation(); } } catch (\Throwable $exception) { echo $exception->getMessage(); }
测试
所有组。
composer tests
功能和单元测试。
composer tests-feature && composer tests-unit
对于集成测试(库)
cp phpunit.xml.dist phpunit.xml
根据 官方文档,使用注册时获得的值填写变量 CLIENT_ID、CLIENT_SECRET 等。在调用获取令牌的方法时,指定 SberPay QR/Плати QR 的 scope。
composer tests-integration