PHP code example of techleeone / oauth2-client

1. Go to this page and download the library: Download techleeone/oauth2-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

techleeone / oauth2-client example snippets


// 如果是使用支持composer自动加载的框架(比如thinkphp,laravel),则无需
$config = [
    // 应用ID | 公众号的唯一标识appid
    'client_id' => '',
    // 应用私钥 |  公众号的appsecret
    'client_secret' => '',
    // 回调地址
    'redirect_uri' => '',
    // 授权登录页面地址
    'authorize_url' => '',
    // 获取AccessToken地址
    'access_token_url' => '',
];
$token = Provider::create($config)->getAccessToken();

$token = Provider::create($config, 'wechat')->getAccessToken();

$token = Provider::create($config, 'oschina')->getAccessToken();


namespace YourNamespace;

use TechOne\OAuth2\Client\Providers\AbstractProvider;
use TechOne\OAuth2\Client\Provider;

class YourOauth2Provider extends AbstractProvider
{
    public function getAuthorizeData()
    {
        return [
            'client_id'     => $this->clientId,
            'redirect_uri'  => $this->redirectUri,
            'response_type' => $this->responseType,
            'state'         => $this->getState(),
        ];
    }

    public function getAccessTokenData($code)
    {
        return [
            'client_id'     => $this->clientId,
            'client_secret' => $this->clientSecret,
            'grant_type'    => 'authorization_code',
            'redirect_uri'  => $this->redirectUri,
            'code'          => $code,
            'dataType'      => 'json',
        ];
    }
}

$token = Provider::create($config, '\\YourNamespace\\YourOauth2Provider')->getAccessToken();