league/oauth1-trello

PHP League OAuth1-Client 的 Trello OAuth 1.0 客户端提供程序

dev-master / 1.0.x-dev 2015-12-28 18:56 UTC

This package is auto-updated.

Last update: 2024-09-14 10:41:27 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

此包为 PHP League 的 OAuth 1.0 客户端 提供了 Trello OAuth 1.0 支持。

安装

要安装,请使用 composer

composer require league/oauth1-trello

用法

用法与 The League 的 OAuth 客户端相同,使用 \League\OAuth1\Client\Server\Trello 作为服务器。

使用 OAuth 1.0 进行身份验证

// Create a server instance.
$server = new \League\OAuth1\Client\Server\Trello([
    'identifier'              => 'your-identifier',
    'secret'                  => 'your-secret',
    'callbackUri'             => 'http://your-callback-uri/',
    // The following can be used to set defaults for the server
    'scope'                   => 'read',
    'expiration'              => '1day',
    'name'                    => 'Trello App'
]);

// Obtain Temporary Credentials and User Authorization
if (!isset($_GET['oauth_token'], $_GET['oauth_verifier'])) {

    // First part of OAuth 1.0 authentication is to
    // obtain Temporary Credentials.
    $temporaryCredentials = $server->getTemporaryCredentials();

    // Store credentials in the session, we'll need them later
    $_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
    session_write_close();

    // Second part of OAuth 1.0 authentication is to obtain User Authorization
    // by redirecting the resource owner to the login screen on the server.
    // Create an authorization url.
    $authorizationUrl = $server->getAuthorizationUrl($temporaryCredentials);

    // Redirect the user to the authorization URL. The user will be redirected
    // to the familiar login screen on the server, where they will login to
    // their account and authorize your app to access their data.
    header('Location: ' . $authorizationUrl);
    exit;

// Obtain Token Credentials
} else {

    try {

        // Retrieve the temporary credentials we saved before.
        $temporaryCredentials = unserialize($_SESSION['temporary_credentials']);

        // We will now obtain Token Credentials from the server.
        $tokenCredentials = $server->getTokenCredentials(
            $temporaryCredentials,
            $_GET['oauth_token'],
            $_GET['oauth_verifier']
        );

        // We have token credentials, which we may use in authenticated
        // requests against the service provider's API.
        echo $tokenCredentials->getIdentifier() . "\n";
        echo $tokenCredentials->getSecret() . "\n";

        // Using the access token, we may look up details about the
        // resource owner.
        $resourceOwner = $server->getResourceOwner($tokenCredentials);

        var_export($resourceOwner->toArray());

        // The server provides a way to get an authenticated API request for
        // the service, using the access token; it returns an object conforming
        // to Psr\Http\Message\RequestInterface.
        $request = $server->getAuthenticatedRequest(
            'GET',
            'http://your.service/endpoint',
            $tokenCredentials
        );

    } catch (\League\OAuth1\Client\Exceptions\Exception $e) {

        // Failed to get the token credentials or user details.
        exit($e->getMessage());

    }

}

配置您的服务器

为了完成与您的用户的授权流程,您需要提供三个额外的信息。

您可以在创建服务器时配置服务器以包含这些信息。

// Create a server instance.
$server = new \League\OAuth1\Client\Server\Trello([
    'identifier'              => 'your-identifier',
    'secret'                  => 'your-secret',
    'callbackUri'             => 'http://your-callback-uri/',
    'scope'                   => 'read',
    'expiration'              => '1day',
    'name'                    => 'Trello App'
]);

您还可以在创建授权 URL 时提供这些信息。

// Create a server instance.
$server = new \League\OAuth1\Client\Server\Trello([
    'identifier'              => 'your-identifier',
    'secret'                  => 'your-secret',
    'callbackUri'             => 'http://your-callback-uri/',
]);

$temporaryCredentials = $server->getTemporaryCredentials();

$options = [
    'scope'                   => 'read',
    'expiration'              => '1day',
    'name'                    => 'Trello App'
];

$authorizationUrl = $server->getAuthorizationUrl($temporaryCredentials, $options);

使用授权 URL 创建时提供的配置将优先于默认配置。

发送一次性请求

您可以使用服务器创建一次性的或简单的认证请求。如果您的需求更加复杂,建议使用 trello-php

$server = new \League\OAuth1\Client\Server\Trello([
    'identifier'              => 'your-identifier',
    'secret'                  => 'your-secret',
    'callbackUri'             => 'http://your-callback-uri/',
]);

$token = 'your-resource-owner-token';
$secret = 'your-resource-owner-secret';

$tokenCredentials = new \League\OAuth1\Client\Credentials\TokenCredentials($token, $secret);

$request = $server->getAuthenticatedRequest(
    'get',
    'https://api.trello.com/1/members/me/boards',
    $tokenCredentials
);

$client = new \GuzzleHttp\Client();

$response = $client->send($request);

运行内置示例

此项目在项目根目录的 example 目录中包含一些示例代码。

首先,打开 example/index.php 文件,并使用您的 Trello 应用标识符和密钥更新服务器配置。

然后,在浏览器中运行代码。使用 PHP 提供的内置服务器可能是最快的方法。在命令行中,在项目根目录运行以下命令。

php -S localhost:9000 -t example

内置的 Web 服务器应该开始运行,当您在浏览器中浏览到 http://localhost:9000 时,您可以开始测试您的配置。

测试

$ ./vendor/bin/phpunit
$ ./vendor/bin/phpcs src --standard=psr2 -sp

贡献

请参阅CONTRIBUTING 获取详细信息。

致谢

许可证

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