Задать вопрос
Ответы пользователя по тегу Mail.Ru
  • Почему получаю ошибку при получении токена Mail.ru?

    doniys_a
    @doniys_a Автор вопроса
    Backend-разработчик (Php, node.js, python, ruby)
    тема закрыта, вот конечный класс для авторизации на mail.ru

    <?php
    
    class Mail extends CApplicationComponent {
    
        public $app_id;
        public $public_key;
        public $secret_key;
        public $private_key;
    
        public $link         = "https://connect.mail.ru/oauth/";
        public $redirectUri  = "http://gethom.com/deploy/mlauth";
        public $responseType = "code";
    
        public $access_token = null;
        public $refresh_token = null;
    
        const TYPE_TOKEN      = 'token';
        const TYPE_CODE_TOKEN = 'code';
    
        public function init() {
    
        }
    
        public function authenticate() {
    
        }
    
        public function getAccessToken($code) {
            $link   = $this->link . "token";
            $params = array(
                'client_id'  => $this->app_id,
                'client_secret' => $this->private_key,
                'grant_type' => 'authorization_code',
                'code' => $code,
                'redirect_uri' => $this->redirectUri,
            );
    
            $response = $this->makeRequest($link, $params);
    
            if (isset($response['access_token'])) {
                $this->access_token  = $response['access_token'];
                $this->refresh_token = $response['refresh_token'];
            }
        }
    
        public function makeRequest($link, $params, $jsonFormat = false) {
            $ch = $this->initRequest($link,  array('data' => $params));
    
            $result = curl_exec($ch);
            $headers = curl_getinfo($ch);
    
            if (curl_errno($ch) > 0) {
                throw new Exception(curl_error($ch), curl_errno($ch));
            }
    
            if ($headers['http_code'] != 200) {
                Yii::log(
                    'Invalid response http code: ' . $headers['http_code'] . '.' . PHP_EOL .
                    'URL: ' . $link . PHP_EOL .
                    'Options: ' . var_export($params, true) . PHP_EOL .
                    'Result: ' . $result,
                    CLogger::LEVEL_ERROR, 'application.extensions.eauth'
                );
                throw new Exception(Yii::t('eauth', 'Invalid response http code: {code}.', array('{code}' => $headers['http_code'])), $headers['http_code']);
            }
    
            curl_close($ch);
    
            return ($jsonFormat) ? $result : CJSON::decode($result);
    
        }
    
        public function loginUrl() {
            $params = array(
                'client_id'     => $this->app_id,
                'response_type' => $this->responseType,
                'redirect_uri'  => $this->redirectUri,
            );
            return ("<a href='" . $this->link . "authorize" . "?" . urldecode(http_build_query($params)) . '\'>Mail.ru</a>');
        }
    
        public function initRequest($url, $options) {
            $ch = curl_init();
            //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // error with open_basedir or safe mode
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    
            if (isset($options['referer'])) {
                curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
            }
    
            if (isset($options['headers'])) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $options['headers']);
            }
    
            if (isset($options['query'])) {
                $url_parts = parse_url($url);
                if (isset($url_parts['query'])) {
                    $query = $url_parts['query'];
                    if (strlen($query) > 0) {
                        $query .= '&';
                    }
                    $query .= http_build_query($options['query']);
                    $url = str_replace($url_parts['query'], $query, $url);
                }
                else {
                    $url_parts['query'] = $options['query'];
                    $new_query = http_build_query($url_parts['query']);
                    $url .= '?' . $new_query;
                }
            }
    
            if (isset($options['data'])) {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $options['data']);
            }
    
            curl_setopt($ch, CURLOPT_URL, $url);
            return $ch;
        }
    
    }

    Код котроллера:
    public function actionMail() {
            echo Yii::app()->mr->loginUrl();
    
            if (isset($_GET['code'])) {
                Yii::app()->mr->getAccessToken($_GET['code']);
                if (Yii::app()->mr->access_token) {
                    echo 'login on mail ru, welcome!';
                }
            }
        }
    Ответ написан
    Комментировать