滴滴物流/oauth2-client

OAuth 2.0 客户端库

0.3.0 2014-04-26 16:55 UTC

This package is auto-updated.

Last update: 2024-09-09 19:47:34 UTC


README

Build Status Coverage Status Total Downloads Latest Stable Version

此软件包使您的应用程序与 OAuth 2.0 身份提供者集成的过程变得非常简单。

每个人都在互联网上看到过“通过 Facebook/Google 等连接”按钮,而社交网络集成现在是大多数 Web 应用程序的一个重要功能。许多这些网站都使用名为 OAuth 2.0 的身份验证和授权标准。

它可以与任何 OAuth 2.0 提供者(无论是您自己的 API 的 OAuth 2.0 服务器还是 Facebook)一起使用,并提供对流行的系统开箱即用的支持。此软件包抽象出不同提供者之间的一些微妙但重要的差异,处理访问令牌和刷新令牌,并允许您轻松访问这些其他网站上的个人资料信息。

此软件包符合PSR-1PSR-2PSR-4。如果您注意到合规性问题,请通过拉取请求发送补丁。

要求

以下版本的 PHP 受支持。

  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • HHVM

用法

授权码流程

$provider = new League\OAuth2\Client\Provider\<ProviderName>(array(
    'clientId'  =>  'XXXXXXXX',
    'clientSecret'  =>  'XXXXXXXX',
    'redirectUri'   =>  'https://your-registered-redirect-uri/'
));

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

    // If we don't have an authorization code then get one
    header('Location: '.$provider->getAuthorizationUrl());
    exit;

} else {

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

    // If you are using Eventbrite you will need to add the grant_type parameter (see below)
    $token = $provider->getAccessToken('authorization_code', [
    	'code' => $_GET['code'],
    	'grant_type' => 'authorization_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
        $userDetails = $provider->getUserDetails($token);

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

    } catch (Exception $e) {

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

    // Use this to interact with an API on the users behalf
    echo $token->accessToken;

    // Use this to get a new access token if the old one expires
    echo $token->refreshToken;

    // Number of seconds until the access token will expire, and need refreshing
    echo $token->expires;
}

刷新令牌

$provider = new League\OAuth2\Client\Provider\<ProviderName>(array(
    'clientId'  =>  'XXXXXXXX',
    'clientSecret'  =>  'XXXXXXXX',
    'redirectUri'   =>  'https://your-registered-redirect-uri/'
));

$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

内置提供者

此软件包目前内置了对以下提供者的支持:

  • Eventbrite
  • Facebook
  • Github
  • Google
  • Instagram
  • LinkedIn
  • Microsoft

这些是我们计划官方支持的 OAuth 2 服务数量。维护广泛的提供者会损害我们使此软件包达到最佳状态的能力,尤其是在我们向 1.0 版本迈进的过程中。

第三方提供者

如果您想支持其他提供者,请将其作为 Composer 软件包提供,然后在其下方链接。

这些提供者允许与 oauth2-client 不支持的提供者进行集成。它们可能需要较旧版本,如果您发现这种情况,请通过拉取请求帮助他们。

客户端软件包

一些开发者将此库用作他们自己的 PHP API 封装库的基础,这听起来是一个非常好的主意。这可能使将他们的提供者与现有的通用“OAuth 2.0 全部”登录系统集成变得稍微有些棘手,但这确实使他们更容易与之合作。

安装

通过 Composer

{
    "require": {
        "league/oauth2-client": "~0.3"
    }
}

测试

$ phpunit

贡献

有关详细信息,请参阅CONTRIBUTING

致谢

许可

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