
本文详解解决 PHP 快速入门中 redirect_uri_mismatch 和 missing the required redirect URI 错误的核心方法,重点在于使用 OAuth 2.0 “Desktop App” 类型客户端 ID、正确配置 credentials.json,并确保 CLI 运行模式与授权流程严格匹配。
本文详解解决 php 快速入门中 `redirect_uri_mismatch` 和 `missing the required redirect uri` 错误的核心方法,重点在于使用 **oauth 2.0 “desktop app” 类型客户端 id**、正确配置 `credentials.json`,并确保 cli 运行模式与授权流程严格匹配。
Google People API 的 PHP 快速入门(官方文档)默认面向命令行(CLI)桌面应用场景,而非 Web 应用。你遇到的两个关键错误——
- Uncaught InvalidArgumentException: missing the required redirect URI
- Error 400: redirect_uri_mismatch
——根本原因并非代码逻辑缺陷,而是 Google Cloud Console 中创建的 OAuth 凭据类型不匹配。
✅ 正确配置步骤(关键四步)
1. 创建「Desktop App」类型的 OAuth 凭据(不可替代!)
- 登录 Google Cloud Console → 选择/创建项目 → 启用 People API。
- 进入 API 和服务 > 凭据 > 创建凭据 > OAuth 客户端 ID。
-
务必选择应用类型为 Desktop app(非 Web application!)
⚠️ 若误选 Web application,系统会强制要求填写 Authorized redirect URIs,且仅接受 http://localhost:*/... 或 https:// 域名;而 Desktop app 类型不需也不允许填写任何 redirect URI,其默认重定向地址为 urn:ietf:wg:oauth:2.0:oob(即“out-of-band”,手动复制粘贴授权码)。
2. 下载并使用正确的 credentials.json
- 创建成功后,点击下载 JSON 文件(文件名如 client_secret_XXXXX.apps.googleusercontent.com.json)。
- 重命名为 credentials.json,并置于项目根目录(与 quickstart.php 同级)。
- ✅ 验证:打开该 JSON,确认其结构包含 "installed" 或 "desktop" 字段(而非 "web"),例如:
{ "installed": { "client_id": "1234567890-abcde1234567890abcdef1234567890ab.apps.googleusercontent.com", "project_id": "your-project-id", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_secret": "AbCdEfGhIjKlMnOpQrStUvWxYz", "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"] } }? 若文件含 "web" 字段或无 "installed",说明你创建的是 Web 类型凭据——必须删除并重新创建 Desktop 类型。
3. 严格保持 CLI 运行模式(禁止浏览器直接访问 .php 文件)
你的代码开头已包含防护:
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
✅ 这是正确设计。请始终通过终端执行:
读取并分析Google表格数据。当用户提到“Google表格”、“电子表格”、“表格”,或发送 docs.google.com/spreadsheets 链接时触发。
php quickstart.php
此时脚本将输出类似以下授权链接:
Open the following link in your browser: https://accounts.google.com/o/oauth2/auth?response_type=code&...
→ 在浏览器中打开该链接 → 登录 Google 账户 → 授权 → 复制页面显示的 6位/8位验证码(Verification code) → 回到终端粘贴并回车。
❌ 错误做法:将 quickstart.php 放在 Apache/Nginx 下用 http://localhost/... 访问 —— 这会触发 Web 流程,但凭据却是 Desktop 类型,必然报 redirect_uri_mismatch。
4. 首次授权后自动生成 token.json,无需手动干预
授权成功后,脚本自动创建 token.json 并持久化 access/refresh token。后续运行将跳过授权步骤,直接调用 API。若需更换账户,删除 token.json 即可重新触发流程。
? 补充说明与最佳实践
-
权限范围(Scopes)建议:
快速入门使用 Google_Service_PeopleService::CONTACTS_READONLY(只读联系人)。如需写入,请改用:$client->setScopes([ 'https://www.googleapis.com/auth/contacts', 'https://www.googleapis.com/auth/userinfo.profile' ]); 避免硬编码 redirect URI:
你的手动添加 $client->setRedirectUri(...) 是 Web 应用配置,在 Desktop 模式下不仅无效,还可能干扰默认 urn:ietf:wg:oauth:2.0:oob 行为,请彻底移除该行。调试技巧:
若仍报错,检查 credentials.json 是否被意外修改,或尝试用 Google API Explorer 手动测试 API 端点,验证凭据有效性。
✅ 总结
| 问题现象 | 根本原因 | 解决方案 |
|---|---|---|
| missing the required redirect URI | 使用了 Web 类型凭据(credentials.json 含 "web" 字段) | 重新创建 Desktop app 类型凭据,确保 JSON 含 "installed" |
| redirect_uri_mismatch | 用浏览器访问 CLI 脚本,或凭据类型错误 | 严格使用 php quickstart.php 终端执行,禁用 Web 访问 |
完成上述配置后,脚本将稳定获取联系人列表,为构建联系人同步、CRM 集成等应用打下可靠基础。
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!










