简体   繁体   English

使用 Firebase 函数使用外部 api

[英]Consuming external api with Firebase Functions

I am trying to consume an external api with Firebase Functions, but it gives a time out error when I use OPTIONS, when I use GET to work normally.我正在尝试使用 Firebase 函数使用外部 api,但是当我使用 OPTIONS 时它会出现超时错误,当我使用 GET 正常工作时。 I don't want to use const request = require('request');我不想使用const request = require('request'); or var rp = require('request-promise');var rp = require('request-promise'); , as they are obsolete. ,因为它们已经过时了。 What may be wrong, I await help from colleagues.可能有什么问题,我等待同事的帮助。

const express = require('express');
const cors = require('cors');

const app = express();
// Permitir solicitações de origem cruzada automaticamente
app.use(cors({
    origin: true
}));
//app.use(cors());


app.get('/criarcliente', (req, res) => {

    let uri = "https://api.iugu.com/v1/customers?api_token=<mytoken>";
    let headers = {
        'Content-Type': 'application/json'
    }

    let body = {
        custom_variables: [{
            name: 'fantasia',
            value: 'Dolci Technology'
        }, {
            name: 'vendedor',
        value: ''
        }],
        email: 'teste1@teste1.com',
        name: 'John Dolci',
        phone: 9999999,
        phone_prefix: 66,
        cpf_cnpj: '00000000000',
        cc_emails: 'test@test.com',
        zip_code: '78520000',
        number: '49',
        street: 'Name Street',
        city: 'Guarantã do Norte',
        state: 'MT',
        district: 'Jardim Araguaia'
    }

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

    const https = require('https');

    var req = https.request(options, (resp) => {
        let data = '';
        resp.on('data', (chunk) => {
            data += chunk;
        });
        resp.on('end', () => {
            var result = JSON.parse(data);
            res.send(result);
        });
        console.log("aqui");
    }).on("error", (err) => {
        console.log("Error: " + err.message);
    });


}); ```

There are two points that I would point out to your case.对于你的情况,我要指出两点。 First, you need to confirm that you are using the Blaze plan in your billing.首先,您需要确认您在账单中使用的是 Blaze 计划。 As clarified in the official pricing documentation , in case you are not and are trying to use a non Google-owned service, you won't be able to do it and this will cause an error.正如官方定价文档中所阐明的那样,如果您不是并且正在尝试使用非 Google 拥有的服务,您将无法使用,这将导致错误。

The second point would be to use the axios library within your application.第二点是在您的应用程序中使用axios库。 It's the one that I believe to be most used with Node.js to allow you access to third-party application within Cloud Functions - I would say that it's pretty easy to use as well.我认为它最常与 Node.js 一起使用以允许您访问 Cloud Functions 中的第三方应用程序 - 我想说它也非常易于使用。 You should find a whole example on using it with third-party API here .您应该在此处找到有关将其与第三方 API 一起使用的完整示例。

To summarize, take a look at your pricing, as it's the most common issue to be caused and give it a try with axios , to confirm that you can avoid the error with it.总而言之,请查看您的定价,因为这是最常见的问题,请尝试使用axios ,以确认您可以避免错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase函数和外部API - firebase functions and external API Firebase 函数从外部 API 调用返回错误文本 - Firebase functions return error text from external API call 对 Firebase Cloud Functions (Spark Tier) 中外部 API 的 HTTP 请求被拒绝 - HTTP request to an external API in Firebase Cloud Functions (Spark Tier) refused Firebase 功能和外部节点包 - Firebase Functions and external node packages Firebase 功能和 API 键 - Firebase Functions and API Keys Dialogflow + 外部 API + Google Cloud Functions *无 * Firebase:如何返回履行响应? - Dialogflow + external API + Google Cloud Functions *without* Firebase: how to return fulfillment response? 使用 Firebase Cloud Functions,我可以从外部 API 安排更新以更新 Firestore - Using Firebase Cloud Functions, can I schedule updates from an external API to update Firestore 无法从 firebase 云函数中的外部 api 读取 POST 响应正文 - Cannot read POST response body from external api in firebase cloud functions FCM考虑了针对Firebase的云功能的外部网络 - FCM considered external network on Cloud Functions for Firebase 在没有cors的情况下接收有关Firebase功能的外部请求 - Receive external requests on firebase functions without cors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM