league/oauth1-bitbucket

PHP League OAuth1-Client 的 Bitbucket OAuth 1.0 客户端提供者

dev-master / 1.0.x-dev 2015-12-08 22:39 UTC

This package is auto-updated.

Last update: 2024-09-14 10:35:12 UTC


README

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

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

安装

要安装,请使用 composer

composer require league/oauth1-bitbucket

用法

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

使用 OAuth 1.0 进行认证

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

// 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());

    }

}

测试

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

贡献

有关详细信息,请参阅贡献指南

致谢

许可协议

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