我试图通过网络客户端向我在Firebase中创建的云功能发送https请求。 云功能代码:
const functions = require('firebase-functions');
exports.app = (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST');
console.log('authorization', req.get('authorization'));
res.status(200).send('authorization test');
};
Web客户端代码:
const data = JSON.stringify('/*data to send here...*/');
const url = '/* url endpoint here */'
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener('readystatechange', () => {
if (this.readyState === 4) {
/* success */
console.log(this.responseText);
}
});
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', '/* auth token string here */');
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.send(data);
在firebase功能的firebase控制台日志中,即使我按照Firebase verifyIdToken page中的说明发送授权令牌,我也始终将授权视为未定义。