mamaclub/oauth2

Mamaclub OAuth2 SDK for PHP

此包的规范仓库似乎已消失,因此该包已被冻结。

dev-master / 1.x-dev 2019-05-21 09:13 UTC

This package is auto-updated.

Last update: 2019-10-21 10:09:08 UTC


README

此包使您轻松地将应用程序与OAuth 2.0服务提供商集成。

Gitter Chat Source Code Latest Version Software License Build Status Scrutinizer Coverage Status Total Downloads

我们都习惯于在网上看到那些“通过Facebook/Google等连接”按钮,社交网络集成是目前大多数Web应用程序的一个重要功能。其中许多网站使用OAuth 2.0(RFC 6749)这样的认证和授权标准。

此包符合PSR-1PSR-2PSR-4PSR-7。如果您发现不符合规范,请通过pull request发送补丁。如果您有兴趣为此库做出贡献,请查看我们的贡献指南

需求

以下版本的PHP受到支持。

  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • PHP 7.2
  • PHP 7.3

安装

可以使用Composer安装Facebook PHP SDK。运行以下命令

composer require mamaclub/oauth2

用法

用户个人资料简单GET示例。

$mamaclub = new MamaclubOAuth([
    'clientId' => 'your client id',
    'clientSecret' => 'your client secret',
    'redirectUri' => 'your redirect uri'
]);


// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $mamaclub->getAuthorizationUrl(['state' => 'mmc_login']);

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} else {

    try {

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

        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo 'Access Token: ' . $accessToken->getToken() . "<br>";
        echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
        echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
        echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";

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


    } catch (\Mamaclub\Exception\Exception $e) {

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

    }

}

刷新令牌

$newAccessToken = $mamaclub->getAccessToken('refresh_token', [
    'refresh_token' => $accessToken->getRefreshToken()
]);