定制 worksection/oauth2-worksection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

worksection/oauth2-worksection

最新稳定版本:1.4.3

Composer 安装命令:

composer require worksection/oauth2-worksection

包简介

Worksection OAuth 2.0 support for the PHP League's OAuth 2.0 Client

README 文档

README

This package provides Worksection OAuth 2.0 support for the PHP League's OAuth 2.0 Client. Worksection FAQ about OAuth2.0

Installation

composer require worksection/oauth2-worksection

Usage

use Worksection\OAuth2\Client\Provider\Worksection as WorksectionOauth2;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

$provider = new WorksectionOauth2([
    'clientId'                => 'yourId',   
    'clientSecret'            => 'yourSecret',
    'redirectUri'             => 'https://redirecturl.com/query'
]);


if (!$_REQUEST['code']) {
    $authorizationUrl = $provider->getAuthorizationUrl(['scope' => 'projects_read users_write']);
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: ' . $authorizationUrl);

} elseif ($_REQUEST['state']) {
    if ($_REQUEST['state'] !== $_SESSION['oauth2state']) {
        exit('Invalid state');
    } else {
        unset($_SESSION['oauth2state']);
    }
    
    // Access token
    try {
        $accessToken = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]);
    } catch (IdentityProviderException $e) {
        exit($e->getMessage());
    }

    // Resource info
    try {
        $resourceOwner = $provider->getResourceOwner($accessToken);
    } catch (IdentityProviderException $e) {
        exit($e->getMessage());
    }
    
    //echo 'Access Token: ' . $accessToken->getToken();
    //echo 'Refresh Token: ' . $accessToken->getRefreshToken();
    //echo 'Account URL: ' . $accessToken->getValues()['account_url'];
    //echo 'Expired in: ' . $accessToken->getExpires();
    //echo 'Resource Owners ID: ' . $resourceOwner->getId();
    //echo 'Resource Owners NAME: ' . $resourceOwner->getName();
    //echo 'Resource Owners EMAIL: ' . $resourceOwner->getEmail();
    //echo 'Resource Owners Account URL: ' . $resourceOwner->getAccountUrl();

    // Make some API request using Access Token
    $options = [
        'body' => json_encode([
            'action' => 'get_tasks',
            'page' => '/project/193/'
        ]),
        'headers' => [
            'Content-Type' => 'application/json'
        ]
    ];
    $request = $provider->getAuthenticatedRequest('POST', $resourceOwner->getAccountUrl() . '/api/oauth2', $accessToken, $options);
    try {
        $response = $provider->getParsedResponse($request);
    } catch (IdentityProviderException $e) {
        exit($e->getMessage());
    }
    var_dump($response);
}

For more information see the PHP League's general usage examples.

License

The MIT License (MIT).

统计信息

  • 总下载量: 7.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-13