我似乎在从Firebase云函数中的访存调用中获得预期响应方面遇到问题。我确信这是由于我对响应,诺言等如何工作缺乏了解。
我正在尝试将Atlassian人群的rest api用于SSO。如果我使用邮递员,则可以从请求中获得所需的结果。所以我知道它的一部分正在工作。
促使我使用云功能的原因是,使用fetch进行相同的请求导致来自本地主机的CORS问题。我认为,如果我可以摆脱浏览器的困扰,那么CORS问题将消失。他们有,但我没有得到想要的回应。
我的云功能如下:
const functions = require('firebase-functions');
const fetch = require('node-fetch');
const btoa = require('btoa');
const cors = require('cors')({origin:true});
const app_name = "app_name";
const app_pass = "app_password";
exports.crowdAuthentication = functions.https.onRequest((request,response)=>
{
cors(request,response,() =>{
let _uri = "https://my.server.uri/crowd/rest/usermanagement/1/session";
let _headers = {
'Content-Type':'application/json','Authorization':`Basic ${btoa(`${app_name}:${app_pass}`)}`
}
let _body = {
username: request.body.username,password: request.body.password
}
const result = fetch(_uri,{
method: 'POST',headers: _headers,body: JSON.stringify(_body),credentials: 'include'
})
response.send(result);
})
})
然后我使用fetch到firebase端点并传递用户名/密码在应用程序中进行调用:
fetch('https://my.firebase.endpoint/functionName',body: JSON.stringify({username:"myusername",password:"mypassword"}),headers: {
'Content-Type':'application/json'
}
})
// get the json from the readable stream
.then((res)=>{return res.json();})
// log the response - {size:0,timeout:0}
.then((res)=>
{
console.log('response: ',res)
})
.catch(err=>
{
console.log('error: ',err)
})
感谢您的光临。