首先,确保您有合适的firebase配置,以便您可以使用REST API访问身份验证服务。 其次,你需要验证您的身份验证令牌是否正确,并且在发出POST请求时,将其作为请求header的Authorization属性的值发送。此外,使用axios进行POST请求时,确保将头信息配置为“Content-Type”:“application / json”。
以下是一个简单的例子,向Firebase REST API的身份验证服务发出POST请求:
import axios from 'axios';
const API_KEY = 'your-firebase-api-key';
const SIGN_UP_URL = `https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=${API_KEY}`;
const signUp = async (email, password) => {
try {
const res = await axios.post(SIGN_UP_URL, {
email,
password,
returnSecureToken: true
}, {
headers: {
'Content-Type': 'application/json'
}
});
console.log(res.data); // 返回相应内容
} catch (error) {
console.log(error);
}
}
signUp('user@example.com', 'password');