为何向Firebase发送的认证HTTP请求返回400错误?
问题分析与解决
问题描述
已确认Firebase邮箱注册API地址正确:https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[API_KEY],API密钥也正确,但在Angular的AuthService中调用接口时,持续收到400错误。
请求代码:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; interface AuthResponseData { idToken:string; email:string; refreshToken:string; expiresIn:string; localId:string; } @Injectable({ providedIn: 'root' }) export class AuthService { signUp(){ return this.http.post<AuthResponseData>('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[AIzaSyC4JiHH6Cp1eeudn5Yu5lSkB-_gDfNudeo]', { email: "email@gmail.com", password: "passwordx", returnSecureToken: true }) } constructor(private http:HttpClient) { } }
收到的错误信息:
POST https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=%5BAIzaSyC4JiHH6Cp1eeudn5Yu5lSkB-_gDfNudeo%5D 400
解决方法
问题出在请求URL的key参数上——你把API密钥放在了方括号[]里,这是错误的。官方文档里的[API_KEY]是占位符,实际使用时需要替换成你的真实密钥,并且去掉方括号。
修改后的signUp方法代码:
signUp(){ return this.http.post<AuthResponseData>('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyC4JiHH6Cp1eeudn5Yu5lSkB-_gDfNudeo', { email: "email@gmail.com", password: "passwordx", returnSecureToken: true }) }
这样就能正确传递API密钥,解决400错误。
内容的提问来源于stack exchange,提问作者Joseph
相关产品推荐
相关产品推荐

