我知道每个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);
答案 0 :(得分:1)
您打错了response.end():
request.get(shopRequestUrl, { headers: shopRequestHeaders })
.then((shopResponse) => {
res.end(shopResponse);
})
从链接的文档中可以看到,end()不带参数。它只是结束响应。如果您有要发送的数据,则可能要调用send()来代替。
如果不确定函数的执行方式,还可以使用console.log()
记录消息以准确了解其功能。仅仅希望一堆代码可以正常工作,这是一个好主意-您应该验证它是否按预期的方式工作。
答案 1 :(得分:0)
您用于request.post()
的请求模块是什么
请参阅:https://www.npmjs.com/package/request#promises--asyncawait
我希望您使用的是https://github.com/request/request-promise模块,而不是请求。
答案 2 :(得分:0)
已解决。事实证明,您需要付费计划(Blaze,即付即用)才能访问外部API。我升级并解决了问题。