Firebase Cloud功能通过https.onCall

时间:2018-04-05 06:04:46

标签: firebase google-cloud-functions

尝试通过https.onCall从Web浏览器或使用curl命令调用firebase云功能。我用curl得到以下回复:

{“error”:{“status”:“INVALID_ARGUMENT”,“message”:“错误请求”}}

当我通过浏览器尝试(并在firebase上进行身份验证)时,我收到一个错误:domain ...“在Access-Control-Allow-Origin标头中找不到”,

这可能更多地与http跨域请求有关,而不是firebase。我试图将Access-Control-Allow-Origin标头设置为'*',但无济于事。

由于我试图让https.onCall工作,我使用了一个超级简单的方法来实现服务器功能:

    exports.testClientCall = functions.https.onCall((req, res) => { 
     var jsonObj = {"Founder": "Apple"};
    var json = JSON.stringify(jsonObj);
  response.send(json);
    });

网络浏览器中的客户端功能是:

function getDataFromFirebaseFunction() {
var http = new XMLHttpRequest();
var url = "https://us-central1-<myproject>.cloudfunctions.net/testClientCall";
var params = {"hello":"friend" };
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("X-MyHeader", "123");
http.setRequestHeader("Access-Control-Allow-Origin", "*");
http.setRequestHeader("Access-Control-Allow-Methods", "POST");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);
}

全力以赴通过客户端执行firebase云功能。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">

      <!-- update the version number as needed -->
      <script defer src="/__/firebase/4.12.1/firebase-app.js"></script>
      <!-- include only the Firebase features as you need -->
      <script defer src="/__/firebase/4.12.1/firebase-auth.js"></script>
      <script defer src="/__/firebase/4.12.1/firebase-database.js"></script>
      <script defer src="/__/firebase/4.12.1/firebase-messaging.js"></script>
      <script defer src="/__/firebase/4.12.1/firebase-storage.js"></script>
      <script defer src="https://www.gstatic.com/firebasejs/4.12.1/firebase-functions.js"></script>

      <!-- initialize the SDK after all desired features are loaded -->
      <script defer src="/__/firebase/init.js"></script>   

      <script>
      function executeHttpsCallableUsingClientSDK() {
//Send a name to firebase cloud function, and get response data

        var testClientCall = firebase.functions().httpsCallable('testClientCall');
        testClientCall ({name: "Steve Jobs"}).then(function(result) {
      // Read result of the Cloud Function.
          console.log("Client done with firebase function request, with result:");
          console.log(result);
    });
      }
      </script>
    </head>
    <body>

    </body>
    </html>