Рубрики
Uncategorized

Сеанс обратного вызова авторизации интеграция с платформой get – CI гость Taobao SDK

Автор оригинала: David Wong.

Многие API открытой платформы Taobao можно использовать только после авторизации. Для авторизации требуется адрес обратного вызова.

Через обратный вызов мы можем получить имя пользователя, идентификатор Taobao, время истечения срока действия и другую информацию для последующей разработки.

Здесь мы непосредственно модифицируем индексный метод приветствия. Код выглядит следующим образом:

   public function index()
    {
        $tbnick = get_cookie('taobao_user_nick');
        $etime = get_cookie('expire_time');
        $tbserid = get_cookie('taobao_user_id');
        $session = get_cookie('access_token' );


        $data = array(
            'taobao_user_nick' => $tbnick,
            'expire_time' => $etime,
            'taobao_user_id' => $tbserid,
            'access_token' => $session
        );

        $this->load->view('welcome_message',$data);
    }

Код шаблона, поскольку страница проста, использует чистый CSS-фреймворк.





    
    
    
    < title > Dahua software licensing < / Title >
    
    
    
    
    
    
    



Dahua software licensing

Use guide of Dahua software. Generally, Dahua software needs to be authorized here

< H2 class = "content subhead" > how to use

Because many of Dahua's software calls the API of Taobao open platform, which requires your authorization. The authorization process is very simple. Click the authorization link to log in and save automatically. < a href = "HTTPS: // OAuth. Taobao. COM / authorize? Response" type = code & client "id = 25307802 & redirect" URI = http: // vipapi. Dahuariji. COM / index. PHP / welcome / OAuth & state = vipapi & view = Web "> Click to authorize me < / a >, Please remember your < code > Taobao digital ID < / code > after authorization.

< H2 class = "content subhead" > what to do if you forget the digital ID of Taobao or the authorization expires

We will provide you with a query interface below. You only need to enter Taobao account number or you need to re authorize it once. The authorization is valid for one month. If the authorization fails, please re authorize.

< H2 class = "content subhead" > query results
< input class = "pure-input-1-2" type = "text" name = "tbnick" placeholder = "Taobao user name" > < button type = "submit" class = "pure button pure button primary" onclick = "getoauth()" > query < / button >
< th > Taobao user name < / th > < th > expiration time < / th > < th > Taobao digital ID < / th >
#session
1

If you can't find the messy characters in your name, don't check them, and re authorize them.

Приведенная выше страница предназначена в основном для работы с пользователем, и для удобного отображения информация после авторизации пользователя сохраняется.

https://oauth.taobao.com/authorize?response_type=code&client_id=25307802&redirect_uri=http://vipapi.dahuariji.com/index.php/welcome/oauth&state=vipapi&view=web    

После обычной авторизации пользователей мы получаем необходимый контент.

public function oauth()
    {
        $url = 'https://oauth.taobao.com/token';
        $postfields = array('grant_type' => 'authorization_code',
            'client_ ID '= >' your appkey ',
            'client_secret' = > your appkey ',
            'code' => $_GET['code'],
            'redirect_uri' => 'http://vipapi.dahuariji.com/index.php/welcome/oauth');
        $post_data = '';
        foreach ($postfields as $key => $value) {
            $post_data .= "$key=" . urlencode($value) . "&";
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        //Specify post data
        curl_setopt($ch, CURLOPT_POST, true);
        //Add variable
        curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data, 0, -1));
        $output = curl_exec($ch);
        curl_close($ch);
        $j = json_decode($output);
        $tbnick = $j->taobao_user_nick;
        $etime = $j->expire_time;
        $openuid = $j->taobao_open_uid;
        $tbserid = $j->taobao_user_id;
        $session = $j->access_token;


        $data = array(
            'taobao_user_nick' => $tbnick,
            'expire_time' => $etime,
            'taobao_open_uid' => $openuid,
            'taobao_user_id' => $tbserid,
            'access_token' => $session
        );

        if ($session) {
            set_cookie('taobao_user_nick',$tbnick,2592000);
            set_cookie('expire_time',$etime,2592000);
            set_cookie('taobao_user_id',$tbserid,2592000);
            set_cookie('access_token' , $session,2592000);
            //Warehousing
            $querys = $this->users_model->get_user($tbserid);
            if ($querys->num_rows() == 1)
            {
                $query = $this->users_model->update_user($data);
            }
            else
            {
                $query = $this->users_model->insert_user($data);
            }
            //

        }
        //$this->load->view('welcome_show',$data);
        //Jump to new page display
        redirect("http://vipapi.dahuariji.com/index.php/welcome/showuid/".$tbserid);
    }

Здесь, чтобы показать простые моменты, перейдите на новую страницу и просто покажите следующее.

    
    public function showuid()
{
    $uid = $this->uri->segment(3);
    $data =array('uid' => $uid);
    $this->load->view('welcome_show',$data);
}  

Основные операции с базой данных

  public function get_user_by_name($taobao_user_nick)
    {
        $query = $this->db->get_where('usertoken', array('taobao_user_nick' => $taobao_user_nick), 0,1);
        return $query;
    }

    public function get_user($taobao_user_id)
    {
        $query = $this->db->get_where('usertoken', array('taobao_user_id' => $taobao_user_id), 0,1);
        return $query;
    }

    public function insert_user($data)
    {
        return $this->db->insert('usertoken', $data);
    }

    public function update_user($data)
    {
        return $this->db->replace('usertoken', $data);
    }
    

Такое базовое управление авторизацией сеанса выполнено хорошо.

Оригинал: “https://developpaper.com/authorization-callback-session-get-ci-framework-integration-taobao-guest-sdk/”