upro/oauth2-yammer

The PHP League OAuth2-Client 的 Yammer OAuth 2.0 客户端提供者

v0.1.1 2017-04-05 10:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:37 UTC


README

Latest Version Software License Build Status Total Downloads

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

安装

需求

  • PHP 版本 5.5 或更高。

通过 Composer 安装

通过 Composer 依赖管理工具安装此库是首选方法。

将以下依赖项添加到您的 composer.json

{
  "require": {
    "upro/oauth2-yammer": "~0.1"
  }
}

或者,简单地运行以下命令行来安装最新稳定版本

$ composer require upro/oauth2-yammer

然后,引入 vendor/autoload.php 文件以启用 Composer 提供的自动加载机制。否则,您的应用程序将无法找到此库的类。

用法

用法与 The League 的 OAuth 客户端相同,使用 \UPro\OAuth2\Client\Provider\Yammer 作为提供者。

授权码流

require __DIR__.'/vendor/autoload.php';

$provider = new UPro\OAuth2\Client\Provider\Yammer([
    'clientId'          => '{yammer-client-id}',
    'clientSecret'      => '{yammer-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {
    $authorizationUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authorizationUrl);
    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->getName());

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

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可

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