twetech/oauth2-ninjaone

此包最新版本(1.0)没有可用的许可证信息。

1.0 2024-08-23 22:18 UTC

This package is auto-updated.

Last update: 2024-09-23 22:50:28 UTC


README

本包为PHP League的OAuth 2.0客户端OAuth 2.0 Client提供NinjaOne OAuth 2.0支持。

安装

要安装,请使用composer

composer require twetech/oauth2-ninjaone

用法

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

授权码流程

$provider = new League\OAuth2\Client\Provider\Ninjaone([
    'clientId'          => '{ninjaone-client-id}',
    'clientSecret'      => '{ninjaone-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}