承接 previewtechs/oauth2-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

previewtechs/oauth2-client

Composer 安装命令:

composer require previewtechs/oauth2-client

包简介

Official OAuth2 Client for Preview Technologies

README 文档

README

This package provides Preview Technologies Limited OAuth 2.0 support for the PHP League's OAuth 2.0 Client.

Latest Stable Version License Build Status Code Coverage Code Quality

Installation

To install, use composer:

composer require previewtechs/oauth2-client

Usage

Usage is the same as The League's OAuth client, using \Previewtechs\Oauth2\Client\Provider as the provider.

Authorization Code Flow

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',   // The client secret assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

For further usage of this package please refer to the core package documentation on "Authorization Code Grant".

To get a full list of scopes, please go to List of available OAuth2.0 scopes

Refreshing a Token

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',   // The client secret assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

$existingAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $existingAccessToken->getRefreshToken()
    ]);
    
    //Remove old and save new access token in your database
}

For further usage of this package please refer to the core package documentation on "Refreshing a Token".

Complete Usage

<?php
require "vendor/autoload.php";

session_start();

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',
    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',
    // The client password assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

// 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 = $provider->getAuthorizationUrl();

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    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 {

        // Try to get an access token using the authorization code grant.
        $accessToken = $provider->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 $accessToken->getToken() . "\n";
        echo $accessToken->getRefreshToken() . "\n";
        echo $accessToken->getExpires() . "\n";
        echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n";

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

        var_export($resourceOwner->toArray());

        // We 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 = $provider->getAuthenticatedRequest(
            'GET',
            'https://myaccount.previewtechs.com/api/v1/identity/user-info',
            $accessToken
        );

        $client = new \GuzzleHttp\Client();
        $result = $client->send($request);

        var_export(json_decode($result->getBody()->getContents(), true));

    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

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

    }

}

Contributing

Please see CONTRIBUTING page for details.

Credits

License

The MIT License (MIT)

Notes: heavily inspired from The PHP League Third Party Provider's libraries

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固