psx/oauth2

v4.0.1 2024-05-10 17:32 UTC

This package is auto-updated.

Last update: 2024-09-10 18:18:43 UTC


README

关于

此包提供OAuth2客户端实现,并提供常见的DTO和异常以构建OAuth2服务器实现

使用方法

授权码

<?php

// at first for the authorization code flow you need to redirect your user to the OAuth2 server
AuthorizationCode::redirect('[auth_url]', '[client_id]', '[redirect_url]');

// if the customer returns you can obtain an access token
$client = new \PSX\Http\Client\Client();
$code = new \PSX\OAuth2\Authorization\AuthorizationCode($client, new Url('[token_url]'));
$code->setClientPassword('[client_id]', '[client_secret]');

$accessToken = $code->getAccessToken('[redirect_url]');

// if we have an access token we can request the api using the access token
$header = [
	'Authorization' => TokenAbstract::factory($accessToken)->getHeader()
];

$request  = new GetRequest('[api_url]', $header);
$response = $client->request($request);

if ($response->getStatusCode() == 200) {
    // request worked
}

客户端凭证

<?php

$client = new \PSX\Http\Client\Client();
$code = new \PSX\OAuth2\Authorization\ClientCredentials($client, new Url('[token_url]'));
$code->setClientPassword('[client_id]', '[client_secret]');

$accessToken = $code->getAccessToken();

// work with the access token