LazyDay
10/25/2017 - 5:06 PM

PHP get google token

public function actionLink()
    {
        $url = 'https://accounts.google.com/o/oauth2/auth';
        $redirect_uri = 'http://hh.dev/index.php?r=things/token';
        $client_id = '';

        $params = array(
            'redirect_uri'  => $redirect_uri,
            'response_type' => 'code',
            'client_id'     => $client_id,
            'scope'         => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/cloudprint'
        );

        echo $link = '<p><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Аутентификация через Google</a></p>';
    }

    public function actionToken($code){
        $redirect_uri = 'http://hh.dev/index.php?r=things/token';
        $client_id = '';
        $client_secret = '';
        $params = array(

            'client_id'     => $client_id,
            'client_secret' => $client_secret,
            'redirect_uri'  => $redirect_uri,
            'grant_type'    => 'authorization_code',
            'code'          => $code

    );

        $url = 'https://accounts.google.com/o/oauth2/token';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($curl);
        curl_close($curl);

        $tokenInfo = json_decode($result, true);

        print_r($tokenInfo);

    }