伊人av超碰伊人久久久,免费天堂无码成人av电影,成人麻豆亚洲综合无码精品,久久国产亚洲精品av香蕉

OAuth2開發(fā)指引

更新時(shí)間:4月 20, 2026 / 創(chuàng)建時(shí)間:4月 20, 2026

 php codeigniter使用示例代碼

<?php 
class Oauth extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('string');

        $this->oauth_server = 'http://admin.jeawin.local/';
        
        // 替換成自己的
        $this->client_id = 'XB9IFKiHgbmURoDJ'; 
        // 替換成自己的
        $this->client_secret = '9VDnJGb3IBFcRH1Q8mtlf6pNxLeoSAgy';
        // 替換成自己的
        $this->redirect_url = 'http://admin.jeawin.local/api.php?c=oauth&m=callback&type=jeawin';
    }

    function index(){
        
        $state = random_string('alnum', 10);
        $this->session->set_userdata('test_oauth_state', $state);
        $url = $this->oauth_server . 'oauth/authorize?client_id='.$this->client_id.'&response_type=code&redirect_uri='.rawurlencode($this->redirect_url).'&state='.$state.'&scope=eggs-count%20profile';
        redirect($url);
    }

    function client(){
        // 獲取access_token
        try {
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $url = $this->oauth_server . 'oauth/token';
            // var_dump($url);
            $response = $client->request('POST', $url, array(
                'form_params' => array(
                    'client_id' => $this->client_id,
                    'client_secret' => $this->client_secret,
                    'grant_type' => 'client_credentials',
                )
            ));
            // log_message("error", $code);
            // var_dump($response->getStatusCode());

            $body = $response->getBody()->getContents();

            $obj = json_decode($body);
            if(!is_null($obj)){
                $access_token = $obj->access_token;
                // 保存access_token
                $this->session->set_userdata('test_access_token', $access_token);

                // 通過access_token獲取api數(shù)據(jù)
                $response = $client->request('GET', $this->oauth_server . 'oauth/api', [
                    'headers' => [
                        'Authorization' => 'Bearer ' . $access_token
                    ]
                ]);
                // $composed = new Psr7\AppendStream([$response->getBody()]);
                $composed = $response->getBody()->getContents();

                var_dump($composed);
                // 

            }
        }catch(Exception $e){
            echo $e->getMessage();
        }        
    }

    function callback(){
        // echo 'callback';
        // 檢查state和code
        $state = $this->input->get('state');
        $test_oauth_state = $this->session->userdata('test_oauth_state');
        if($state !== $test_oauth_state){
            $this->output->set_output('state請(qǐng)求參數(shù)錯(cuò)誤');
            return;
        }
        $code = $this->input->get('code');
        if(!isset($code)){
            $this->output->set_output('code參數(shù)錯(cuò)誤');
            return;
        }

        // 獲取access_token
        try {
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $url = $this->oauth_server . 'oauth/token';
            // var_dump($url);
            $response = $client->request('POST', $url, array(
                'form_params' => array(
                    'client_id' => $this->client_id,
                    'client_secret' => $this->client_secret,
                    'grant_type' => 'authorization_code',
                    'redirect_uri' => $this->redirect_url,
                    'code' => $code
                )
            ));
            // log_message("error", $code);
            // var_dump($response->getStatusCode());

            $body = $response->getBody()->getContents();

            $obj = json_decode($body);
            if(!is_null($obj)){
                $access_token = $obj->access_token;
                $refresh_token = $obj->refresh_token;
                // 保存access_token
                $this->session->set_userdata('test_access_token', $access_token);
                $this->session->set_userdata('test_refresh_token', $refresh_token);

                // 通過access_token獲取api數(shù)據(jù)
                $response = $client->request('GET', $this->oauth_server . 'oauth/api', [
                    'headers' => [
                        'Authorization' => 'Bearer ' . $access_token
                    ]
                ]);
                // $composed = new Psr7\AppendStream([$response->getBody()]);
                $composed = $response->getBody()->getContents();

                var_dump($composed);
                // 

            }
        }catch(Exception $e){
            echo $e->getMessage();
        }


    }

    function get_api(){
        $access_token = $this->session->userdata('test_access_token');
        try{
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $response = $client->request('GET', $this->oauth_server . 'oauth/api', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $access_token
                ]
            ]);
            // $composed = new Psr7\AppendStream([$response->getBody()]);
            $composed = $response->getBody()->getContents();
            var_dump($composed);
        }catch(Exception $e){
            var_dump($e->getMessage());
        }

    }

    function get_userinfo(){
        $access_token = $this->session->userdata('test_access_token');
        try{
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $response = $client->request('GET', $this->oauth_server . 'oauth/api/userinfo', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $access_token
                ]
            ]);
            // $composed = new Psr7\AppendStream([$response->getBody()]);
            $composed = $response->getBody()->getContents();
            var_dump($composed);
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }

    function get_forms(){
        $access_token = $this->session->userdata('test_access_token');
        try{
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $response = $client->request('GET', $this->oauth_server . 'oauth/api/forms', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $access_token
                ],
                'query' => [
                    'site_id' => 4
                ]
            ]);
            // $composed = new Psr7\AppendStream([$response->getBody()]);
            $composed = $response->getBody()->getContents();
            var_dump($composed);
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }

    function get_forms_nodes(){
        $form_id = $this->input->get('form_id');
        $page = $this->input->get('page');
        $access_token = $this->session->userdata('test_access_token');
        try{
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $response = $client->request('GET', $this->oauth_server . 'oauth/api/forms_nodes', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $access_token
                ],
                'query' => [
                    'form_id' => $form_id,
                    'page' => $page
                ]
            ]);
            // $composed = new Psr7\AppendStream([$response->getBody()]);
            $composed = $response->getBody()->getContents();
            var_dump($composed);
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }

    function get_site_comments(){
        $page = $this->input->get('page');
        $access_token = $this->session->userdata('test_access_token');
        try{
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $response = $client->request('GET', $this->oauth_server . 'oauth/api/site_comments', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $access_token
                ],
                'query' => [
                    // 'form_id' => $form_id
                    'page' => $page
                ]
            ]);
            // $composed = new Psr7\AppendStream([$response->getBody()]);
            $composed = $response->getBody()->getContents();
            var_dump($composed);
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }

    function refresh_token(){
        $refresh_token = $this->session->userdata('test_refresh_token');
        try{
            $client = new GuzzleHttp\Client(['verify' => FALSE]);
            $response = $client->request('POST', $this->oauth_server . 'oauth/token', [
                'form_params' => [
                    'client_id' => $this->client_id,
                    'client_secret' => $this->client_secret,
                    'grant_type' => 'refresh_token',
                    'refresh_token' => $refresh_token
                ]
            ]);

            $body = $response->getBody()->getContents();
            var_dump($body);
            $obj = json_decode($body);
            if(!is_null($obj)){
                $access_token = $obj->access_token;
                $refresh_token = $obj->refresh_token;
                // 重新保存access_token
                $this->session->set_userdata('test_access_token', $access_token);
                $this->session->set_userdata('test_refresh_token', $refresh_token);
            }
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
    }
}
// end this file

如果您需要與我們?nèi)〉寐?lián)系,以下是我們的聯(lián)系方式

如果您需要與我們?nèi)〉寐?lián)系,以下是我們的聯(lián)系方式
聯(lián)系電話
聯(lián)系郵箱
微信聯(lián)系
杰贏網(wǎng)絡(luò)
QQ
215168