• 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client


    在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client

     
    这个包能够让你以很简单的方式在在PHP应用中集成OAuth2.0身份验证。
    用法

    验证代码流程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    $provider = new LeagueOAuth2ClientProvider<ProviderName>(array(
        'clientId'  =>  'XXXXXXXX',
        'clientSecret'  =>  'XXXXXXXX',
        'redirectUri'   =>  'https://your-registered-redirect-uri/',
        'scopes' => array('email', '...', '...'),
    ));
     
    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;
    }

    Refreshing a Token

    1
    2
    3
    4
    5
    6
    7
    8
    $provider = new LeagueOAuth2ClientProvider<ProviderName>(array(
        'clientId'  =>  'XXXXXXXX',
        'clientSecret'  =>  'XXXXXXXX',
        'redirectUri'   =>  'https://your-registered-redirect-uri/'
    ));
     
    $grant = new LeagueOAuth2ClientGrantRefreshToken();
    $token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

    Built-In Providers

    这个包目前已经内置了支持:

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

    项目主页:http://www.open-open.com/lib/view/home/1417502997514

  • 相关阅读:
    yb课堂 vue里面的状态管理vuex 《四十》
    yb课堂 注册-登陆-个人模块 《三十九》
    MySQL 获取所有表名、所有表结构
    Window版 MySQL可视化工具 Navicat 面安装免激活绿色版
    yb课堂 视频详情页模块开发《三十八》
    yb课堂 首页home开发 《三十七》
    yb课堂 前端项目通用底部选项卡 CommonsFooter 《三十六》
    yb课堂 开发前端项目路由 《三十五》
    yb课堂 基于浏览器和node.js的http客户端Axios 《三十四》
    2020 团体程序设计天梯赛 个人题解 + 反思
  • 原文地址:https://www.cnblogs.com/ldms/p/4565731.html
Copyright © 2020-2023  润新知