mglinski/oauth2-eveonline

为 PHP League OAuth2-Client 提供的 EvE Online OAuth 2.0 客户端提供程序

2.2.1 2021-02-27 17:41 UTC

This package is auto-updated.

Last update: 2024-09-17 07:29:54 UTC


README

Latest Version Software License Build Status Code Coverage Scrutinizer Code Quality Total Downloads

此软件包为 PHP League 的 OAuth 2.0 客户端 提供EvE Online OAuth 2.0 支持。

安装

要安装,请使用 composer

composer require evelabs/oauth2-eveonline

文档

EvE Online CREST&SSO 第三方文档

使用

使用方法与 League 的 OAuth 客户端相同,使用 \Evelabs\OAuth2\Client\Provider\EveOnline 作为提供程序。

请参阅 example/example.php 以获取更多信息。

授权码流

$provider = new Evelabs\OAuth2\Client\Provider\EveOnline([
    'clientId'          => '{eveonline-client-id}',
    'clientSecret'      => '{eveonline-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']
    ]);

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getCharacterName());

    } catch (Exception $e) {

        // Failed to get user details
        exit('Oh dear...');
    }

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

管理作用域

在创建您的 EveOnline 授权 URL 时,您可以指定应用程序可能授权的作用域。

$options = [
    'scope' => ['publicData','characterLocationRead'] // array or string
];

$authorizationUrl = $provider->getAuthorizationUrl($options);

刷新访问令牌

EVE Online Oauth 服务器发出短期(约 20 分钟)的访问令牌,因此一旦它过期,您必须使用长期刷新令牌来获取一个新的令牌。

$new_token = $provider->getAccessToken('refresh_token', [
    'refresh_token' => $old_token->getRefreshToken()
]);

调用 CREST

一旦您获得了(访问和刷新)令牌,您就可以开始发出请求。

示例 GET

$request = $provider->getAuthenticatedRequest(
    'GET',
    'https://crest-tq.eveonline.com/characters/{characterID}/',
    $accessToken->getToken()
);

$response = $provider->getResponse($request);

示例 POST

$request = $provider->getAuthenticatedRequest(
    'POST',
    'https://crest-tq.eveonline.com/characters/{characterID}/',
    $accessToken->getToken(),
    ['body' => 'some stuff']
);

$response = $provider->getResponse($request);

框架集成

Symfony 2

测试

$ ./vendor/bin/phpunit

贡献

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

致谢

许可证

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