具有对外部api的基本身份验证的获取请求的Firebase云功能

我似乎在从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)
        })

感谢您的光临。

tian150288 回答:具有对外部api的基本身份验证的获取请求的Firebase云功能

在下面的评论中我们的讨论之后进行更新

似乎node-fetch库不起作用,您应该使用request-promise之类的其他库。

因此,您应该按照以下方式修改代码:

//......
var rp = require('request-promise');


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
        }

        var options = {
            method: 'POST',uri: _uri,body: _body,headers: _headers,json: true
        };

        rp(options)
            .then(parsedBody => {
                response.send(parsedBody);
            })
            .catch(err => {
                response.status(500).send(err)
                //.... Please refer to the following official video: https://www.youtube.com/watch?v=7IkUgCLr5oA&t=1s&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=3
            });

    });

});

通过节点获取的初始答案

fetch()方法是异步的,并返回Promise。因此,您需要等待此Promise解析后再发送回响应,如下所示:

exports.crowdAuthentication = functions.https.onRequest((request,response)=>
{
    cors(request,() =>{

        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
        }

        fetch(_uri,{
            method: 'POST',body: JSON.stringify(_body),credentials: 'include'
        })
        .then(res => {
          res.json()
        })
        .then(json => {
          response.send(json);
        }
        .catch(error => {
            //.... Please refer to the following official video: https://www.youtube.com/watch?v=7IkUgCLr5oA&t=1s&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=3
        });

    })
})

此外,请注意,您需要加入“火焰”或“烈火”定价计划。

事实上,免费的“ Spark”计划“仅允许对Google拥有的服务进行出站网络请求”。请参见https://firebase.google.com/pricing/(将鼠标悬停在“云函数”标题之后的问号上)

本文链接:https://www.f2er.com/3150619.html

大家都在问