调用Google登录API时出现‘Legacy People API未在项目中使用或已禁用’错误的解决及代码问题分析
嘿,我来帮你搞定这两个问题——先解决那个Legacy People API的错误,再梳理你PHP代码里的潜在问题:
解决Legacy People API错误及PHP代码问题分析
一、搞定"Legacy People API has not been used in project..."错误
虽然你确认People API已经启用,但这个错误通常是由几个容易忽略的点导致的,按下面的步骤排查:
- 核对项目ID是否匹配:错误提示里的项目ID
************必须和你当前用的app_key(API密钥)所属的项目完全一致。很多时候是不小心拿了其他项目的API密钥,导致系统检测到这个项目没启用该API。 - 给API一点生效时间:Google API启用后不是立刻就能用的,一般需要5-15分钟的传播时间,别急,等一会儿再测试试试。
- 检查OAuth授权范围(Scope):获取
access_token的时候,必须请求People API的相关权限,比如:https://www.googleapis.com/auth/userinfo.profile(获取用户基本资料)https://www.googleapis.com/auth/userinfo.email(获取用户邮箱)https://www.googleapis.com/auth/people.readonly(只读访问用户个人资料)
如果Scope不对,哪怕API启用了,也会触发权限相关的错误。
- 确认启用的是正确的API:确保你在Google Cloud控制台启用的是People API(不是那个Legacy People API,后者是旧的Google+相关接口,已经被废弃了),直接在API库搜"People API"启用就好。
二、你的PHP代码里的坑我都找出来了
我仔细看了你的代码,有几个关键问题得修正:
$access_token变量没定义!
代码里直接用了$access_token但完全没赋值,这会导致URL里的access_token参数是空的,请求肯定失败。你得确保这个变量是用户登录后拿到的有效OAuth 2.0访问令牌。URL里的
&是错的&是HTML里的转义字符,在PHP的URL字符串里直接用&就行,不然服务器会把&当成URL的一部分,导致key参数无法被正确识别。正确的URL应该是:"https://people.googleapis.com/v1/people/me?access_token={$access_token}&key={$app_key}"错误处理太粗糙
当API返回错误时,$json_data->error是个对象,直接把它赋值给$error_message会输出一堆对象信息,不是用户能看懂的错误文本。得提取具体的错误消息:$error_message = $json_data->error->message;没检查字段是否存在就直接访问
你直接取$json_data->emails[0]->value,但如果用户没授权邮箱权限,emails字段可能根本不存在,这会触发PHP警告。加个判断更安全:$social_email = ''; if (!empty($json_data->emails) && is_array($json_data->emails) && isset($json_data->emails[0]->value)) { $social_email = $json_data->emails[0]->value; }没处理网络请求失败的情况
fetchDataFromURL函数可能因为网络问题返回false或者非JSON字符串,这时候json_decode会返回null,后面的逻辑全乱了。先检查请求结果:$get_user_details = fetchDataFromURL($url); if (!$get_user_details) { $error_code = 6; $error_message = 'Failed to connect to Google API'; } else { $json_data = json_decode($get_user_details); if (json_last_error() !== JSON_ERROR_NONE) { $error_code = 7; $error_message = 'Invalid response from Google API'; } // 后面的逻辑... }
修正后的代码片段参考
我把这些问题都修复了,你可以参考:
if (empty($_POST['google_key'])) { $error_code = 5; $error_message = 'google_key (POST) is missing'; } elseif (empty($access_token)) { // 新增:检查access_token是否存在 $error_code = 8; $error_message = 'access_token is missing'; } else { $app_key = $_POST['google_key']; // 修正URL中的&符号 $url = "https://people.googleapis.com/v1/people/me?access_token={$access_token}&key={$app_key}"; $get_user_details = fetchDataFromURL($url); if (!$get_user_details) { $error_code = 6; $error_message = 'Failed to connect to Google API'; } else { $json_data = json_decode($get_user_details); if (json_last_error() !== JSON_ERROR_NONE) { $error_code = 7; $error_message = 'Invalid JSON response from Google'; } elseif (!empty($json_data->error)) { $error_code = 4; $error_message = $json_data->error->message; } elseif (!empty($json_data->id)) { $social_id = $json_data->id; $social_email = ''; // 安全获取邮箱 if (!empty($json_data->emails) && is_array($json_data->emails) && isset($json_data->emails[0]->value)) { $social_email = $json_data->emails[0]->value; } $social_name = $json_data->displayName ?? ''; // 用null合并运算符避免未定义字段警告 if (empty($social_email)) { $social_email = 'go_' . $social_id . '@google.com'; } } else { $error_code = 9; $error_message = 'No valid user data returned from Google'; } } }
内容的提问来源于stack exchange,提问作者vainu
相关产品推荐
相关产品推荐

