You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中Google登录报403错误:Legacy People API异常

解决CodeIgniter中Google登录403(Legacy People API禁用)问题

你的错误核心是Google+ API已完全停用,代码中使用的Google_Service_Plus类以及plus.login、plus.me等授权范围都是已废弃的资源,即使启用了新的People API,旧代码调用的还是已关闭的Plus端点,因此触发403错误。

解决方案

步骤1:替换API服务类与授权范围

将废弃的Plus服务替换为PeopleService,并移除无效的授权范围,调整用户信息获取逻辑:

<?php 
require_once('Google/autoload.php');

class Google {
    protected $CI;

    public function __construct($url){
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->config->load('google_config');
        $this->client = new Google_Client();
        $this->client->setClientId($this->CI->config->item('google_client_id'));
        $this->client->setClientSecret($this->CI->config->item('google_client_secret'));
        $this->client->setRedirectUri($url[0]);
        // 保留必要的有效授权范围
        $this->client->setScopes(array(
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile"
        ));
        // 可选:设置offline模式支持刷新token
        $this->client->setAccessType("offline");
    }

    public function get_login_url(){
        return  $this->client->createAuthUrl();
    }

    public function validate(){     
        if (isset($_GET['code'])) {
          $this->client->authenticate($_GET['code']);
          $_SESSION['access_token'] = $this->client->getAccessToken();
        }
        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
          $this->client->setAccessToken($_SESSION['access_token']);
          
          // 替换为PeopleService服务类
          $peopleService = new Google_Service_PeopleService($this->client);
          // 指定需要获取的用户字段,减少不必要的数据请求
          $person = $peopleService->people->get('people/me', array(
              'personFields' => 'names,emailAddresses,photos'
          ));
          
          // 从新的API返回结构中提取用户信息
          $info['id'] = $person->getResourceName();
          $info['email'] = $person->getEmailAddresses()[0]->getValue();
          $info['name'] = $person->getNames()[0]->getDisplayName();
          $info['profile_pic'] = $person->getPhotos()[0]->getUrl();

          return $info;
        }
        return null;
    }
}

步骤2:确认People API状态

  1. 登录Google Cloud控制台,定位到你的项目
  2. 在API库中搜索People API(注意区分旧版Legacy People API)
  3. 确保API状态为「已启用」

步骤3:清理授权缓存

  • 清除用户浏览器中与Google登录相关的Cookie和Session
  • 清空服务器端的Session存储,避免旧access token干扰

补充说明

  • 若需要获取更多用户信息,可修改personFields参数(如添加birthdays,locations),但需同步添加对应的授权范围
  • 建议使用Composer管理Google API客户端库,替代手动引入autoload.php,避免版本过期问题

内容的提问来源于stack exchange,提问作者user3653474

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:25:29