fathershawn/oauth2-formassembly

PHP League OAuth2-Client 的 FormAssembly OAuth 2.0 客户端提供者

1.2 2022-12-12 12:44 UTC

This package is auto-updated.

Last update: 2024-09-12 16:47:07 UTC


README

此包为 PHP League 的 FormAssembly OAuth 2.0 支持提供 OAuth 2.0 客户端。

安装

要安装,请使用 composer

composer require fathershawn/oauth2-formassembly

使用方法

请求授权代码

如果您正在使用 FormAssembly,可能已经有了一个同名类,因此您可以重命名提供者

<?php
use Fathershawn\OAuth2\Client\Provider\FormAssembly as OauthProvider;

$provider = new OauthProvider([
      'clientId' => 'your-client-id',
      'clientSecret' => 'your-client-secret',
      'redirectUri' => 'url-to-capture-the-code',
      'baseUrl' => 'url-to-formassembly-instance',
]);
$url = $provider->getAuthorizationUrl();
// Redirect the user to $url.

当用户被重定向到捕获代码的 URL 时

<?php

use Fathershawn\OAuth2\Client\Provider\FormAssembly as OauthProvider;
// Capture the code with your framework or from $_GET['code'].
$code = $_GET['code'];
$provider = new OauthProvider([
      'clientId' => 'your-client-id',
      'clientSecret' => 'your-client-secret',
      'redirectUri' => 'url-to-capture-the-code',
      'baseUrl' => 'url-to-formassembly-instance',
]);
try {
    $accessToken = $provider->getAccessToken('authorization_code', [
        'code' => $code,
    ]);
    // Store $accessToken as appropriate for re-use.
} catch (Exception $e) {
    // Log the Exception?
    throw $e;
}