使用GCP Identity Platform提示API Key无效,但密钥确认正确
问题描述
按照教程使用GCP Identity Platform,从平台界面复制了确认无误的API Key,但调用signInWithEmailAndPassword接口时返回400错误,提示“API Key not found. Please pass a valid API key”。尝试使用firebase-ui-auth也出现相同问题,已完成前置要求仍未找到问题原因。
最小复现代码:
<!DOCTYPE html> <html> <head> <title>Identity Platform Test</title> <script src="https://www.gstatic.com/firebasejs/8.0/firebase.js"></script> <script> //this section copied directly from GCP var config = { apiKey: '<Private but 100% correct>', authDomain: 'my-site.firebaseapp.com', }; firebase.initializeApp(config); </script> <script> let email = 'private@gmail.com'; let pw = 'private'; firebase .auth() .signInWithEmailAndPassword(email, pw) .then((ac) => { console.log('It worked.'); }) .catch((error) => { console.log(error.toString()); }); </script> </head> <body> Test </body> </html>
排查与解决建议
- 检查API Key的限制配置:登录GCP控制台,找到对应的API Key,确认是否设置了IP限制、HTTP referrer限制,或只允许特定API使用该Key。若设置了referrer限制,需确保当前测试页面的域名在允许列表内;本地测试可临时添加
localhost或127.0.0.1到允许列表。 - 确认API Key关联项目匹配:核对复制的API Key所属的GCP项目,是否和
authDomain对应的项目一致,避免误复制其他项目的Key导致验证失败。 - 验证Firebase SDK版本兼容性:当前使用的是Firebase 8.0版本,可尝试升级到较新的稳定版本(如8.10.0),排查版本兼容问题。
- 排查网络环境干扰:本地代理、防火墙可能拦截API请求导致Key无法正常传递,尝试关闭代理或切换网络环境测试。
- 清空浏览器缓存:浏览器缓存的旧配置可能干扰验证,清空缓存后重新加载页面测试。
- 用REST API直接验证:绕过Firebase SDK,用Postman等工具调用Identity Platform的
signInWithPasswordREST接口并传入API Key,验证请求是否正常,以此区分是SDK配置问题还是Key本身的问题。
内容的提问来源于stack exchange,提问作者WillyC
相关产品推荐
相关产品推荐

