【问题标题】:Post Request Firebase Cloud Functions Timing Out发布请求 Firebase 云功能超时
【发布时间】:2018-10-22 06:58:34
【问题描述】:

我知道每个 HTTP 函数都必须以 end() 或 send() 结尾,所以我认为这可能与我的问题有关。我正在构建一个我想在 Firebase 上托管的 Shopify 应用。我已经让它进行身份验证和安装,但是当我尝试通过 POST 捕获永久访问令牌时,Firebase 超时。同样的代码适用于 ngrok。整个路由函数如下。

const dotenv = require('dotenv').config();
const functions = require('firebase-functions');
const express = require('express');

const app = express();
const crypto = require('crypto');
const cookie = require('cookie');
const nonce = require('nonce')();
const querystring = require('querystring');
const request = require('request-promise');

const apiKey = process.env.SHOPIFY_API_KEY;
const apiSecret = process.env.SHOPIFY_API_SECRET;
const scopes = 'read_products,read_customers';
const forwardingAddress = 'https://my-custom-app.firebaseapp.com/app';

app.get('/app/shopify/callback', (req, res) => {
  const { shop, hmac, code, state } = req.query;
  const stateCookie = cookie.parse(req.headers.cookie).__session;

  if (state !== stateCookie) {
    return res.status(403).send('Request origin cannot be verified');
  }

  if (shop && hmac && code) {
    // DONE: Validate request is from Shopify
    const map = Object.assign({}, req.query);
    delete map['signature'];
    delete map['hmac'];
    const message = querystring.stringify(map);

  const generatedHash = crypto
    .createHmac('sha256', apiSecret)
    .update(message)
    .digest('hex');

  if (generatedHash !== hmac) {
    return res.status(400).send('HMAC validation failed');
  }


  // Collect permanent access token
  const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
  const accessTokenPayload = {
    client_id: apiKey,
    client_secret: apiSecret,
    code,
  };

  // Everything works up until here

  request.post(accessTokenRequestUrl, { json: accessTokenPayload })
    .then((accessTokenResponse) => {
        const accessToken = accessTokenResponse.access_token;

        // If below is uncommented, it will not show on browser, Firebase seems to timeout on the above request.post.
        //res.status(200).send("Got an access token, let's do something with it");

        // Use access token to make API call to 'shop' endpoint
        const shopRequestUrl = 'https://' + shop + '/admin/shop.json';
        const shopRequestHeaders = {
            'X-Shopify-Access-Token': accessToken,
        };

        request.get(shopRequestUrl, { headers: shopRequestHeaders })
            .then((shopResponse) => {
                res.end(shopResponse);
            })
            .catch((error) => {
                res.status(error.statusCode).send(error.error.error_description);
            });

    })
        .catch((error) => {
            res.status(error.statusCode).send(error.error.error_description);
        });
  } else {
res.status(400).send('Required parameters missing');
  }
});

exports.shopifyValidate = functions.https.onRequest(app);

【问题讨论】:

标签: node.js firebase express google-cloud-functions shopify


【解决方案1】:

你打错了response.end()

    request.get(shopRequestUrl, { headers: shopRequestHeaders })
        .then((shopResponse) => {
            res.end(shopResponse);
        })

从链接文档中可以看出,end() 不带参数。它只是结束响应。如果您有数据要发送,您可能希望调用 send()。

如果您不确定您的函数是如何执行的,也可以使用console.log() 记录消息以准确了解它在做什么。仅仅希望一堆代码能正常工作几乎不是一个好主意——您应该验证它是否按您期望的方式工作。

【讨论】:

  • 我一直在关注 Shopify 教程,并在此过程中运行该应用程序,到目前为止它一直在工作。事实上,我拥有的代码使用 ngrok 隧道工作,只是不适用于 Firebase。 Shopify tut 使用 res.end(shopResponse),所以我认为这不是问题,但我还是会尝试一下。教程:help.shopify.com/en/api/tutorials/building-node-app
  • 哎呀,我刚才已将其标记为正确,但我仍然在应用程序设置中列出了 ngrok url,这就是它起作用的原因。我切换回 Firebase 网址,但仍然遇到同样的问题。
【解决方案2】:

解决了。事实证明,您需要付费计划(Blaze,即用即付)来访问外部 API。我升级了,解决了这个问题。

【讨论】:

    【解决方案3】:

    您为request.post() 使用的请求模块是什么? 请看:https://www.npmjs.com/package/request#promises--asyncawait

    我希望您使用https://github.com/request/request-promise 模块而不是请求。

    【讨论】:

    • 我刚刚更新了代码以包含我的所有要求。是的,我正在使用 request-promise。
    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 2018-05-17
    • 2017-10-10
    • 1970-01-01
    • 2017-07-31
    • 2019-10-17
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多