public function execute()
 {
     if (!$this->checkRequest()) {
         return;
     }
     $code = waRequest::post('code');
     $auth_codes_model = new waApiAuthCodesModel();
     $row = $auth_codes_model->getById($code);
     if ($row) {
         // check client_id
         if ($row['client_id'] != waRequest::post('client_id')) {
             $this->response(array('error' => 'invalid_grant'));
             return;
         }
         // check expire
         if (strtotime($row['expires']) < time()) {
             $this->response(array('error' => 'invalid_grant', 'error_description' => 'Authorization code expired'));
             return;
         }
         // create token
         $token_model = new waApiTokensModel();
         $token = $token_model->getToken($row['client_id'], $row['contact_id'], $row['scope']);
         $this->response(array('access_token' => $token));
     } else {
         $this->response(array('error' => 'invalid_grant', 'error_description' => 'Invalid code: ' . $code));
     }
 }
 protected function approve()
 {
     $url = waRequest::get('redirect_uri');
     if ($this->response_type == 'token') {
         $token_model = new waApiTokensModel();
         $token = $token_model->getToken($this->client_id, $this->contact_id, waRequest::get('scope'));
         $this->redirect($url . '#access_token=' . $token);
     } elseif ($this->response_type == 'code') {
         $code = $this->createAuthCode();
         // redirect
         if ($url) {
             $this->redirect($url . (strpos($url, '?') === false ? '?' : '&') . 'code=' . $code);
         } else {
             $this->view->assign('code', $code);
         }
     }
 }