php 使用google登录如何实现

1.去谷歌网站申请对应的应用,获得客户端id和密钥,配置回调地址

https://console.cloud.google.com/auth/clients?inv=1&invt=Ab0fZA&project=cogent-dragon-436505-d2

2.登录入口页面先调用这个方法

  public function ggLogin(){

        session_start();

// 配置信息
        $client_id = '661679842095-vc959rkgenbg51eg5s74sb0uk9qj8.apps.googleuse.com';
        $redirect_uri = 'https://test.1688order.com/api/account/gLogin_callback';
        $scope = 'openid email profile';

// 生成随机的state参数防止CSRF攻击
        $_SESSION['oauth_state'] = bin2hex(random_bytes(16));

// 构建Google授权URL
        $auth_url = 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query([
                'response_type' => 'code',
                'client_id' => $client_id,
                'redirect_uri' => $redirect_uri,
                'scope' => $scope,
                'state' => $_SESSION['oauth_state'],
                'access_type' => 'online',
                'prompt' => 'consent'
            ]);

// 重定向到Google登录页面
        header('Location: ' . $auth_url);
        exit;

    }

3.回调函数的调用,根据返回的code获取token,再根据token获取用户信息

    //谷歌登录回调方法
    public function gLogin_callback(){

        session_start();

// 验证state参数防止CSRF攻击
        if (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['oauth_state']) {
            die('Invalid state parameter');
        }

// 配置信息
        $client_id = '661679842095-vc959rkgenbg51eg5s74sb0uk9qj8.apps.gusercontent.com';
        $client_secret = 'GOCSPX-JASjC6g1LREoSVRGpq5Uw5j';
        $redirect_uri = 'https://test.1688order.com/api/account/gLogin_callback';

// 获取授权码
        if (!isset($_GET['code'])) {
            die('Authorization code not found');
        }
        $code = $_GET['code'];

// 1. 使用授权码获取访问令牌
        $token_url = 'https://oauth2.googleapis.com/token';
        $token_data = [
            'code' => $code,
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'redirect_uri' => $redirect_uri,
            'grant_type' => 'authorization_code'
        ];

        $ch = curl_init($token_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_data));
        $token_response = curl_exec($ch);
        curl_close($ch);

        $token_info = json_decode($token_response, true);
        if (isset($token_info['error'])) {
            die('Token error: ' . $token_info['error_description']);
        }

        $access_token = $token_info['access_token'];


// 2. 使用访问令牌获取用户信息
        $userinfo_url = 'https://openidconnect.googleapis.com/v1/userinfo';
        $ch = curl_init($userinfo_url . '?access_token=' . urlencode($access_token));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $userinfo_response = curl_exec($ch);
        curl_close($ch);

        $userinfo = json_decode($userinfo_response, true);
        if (isset($userinfo['error'])) {
            die('Userinfo error: ' . $userinfo['error_description']);
        }

        print_r($userinfo);die;
// 3. 处理用户信息
        $_SESSION['google_user'] = [
            'id' => $userinfo['sub'],
            'email' => $userinfo['email'],
            'name' => $userinfo['name'] ?? '',
            'picture' => $userinfo['picture'] ?? ''
        ];

// 登录成功,跳转到用户主页
        header('Location: /user-profile.php');
        exit;


    }

你可能感兴趣的:(php,开发语言)