skip to Main Content

I know that each HTTP function must end with end() or send(), so I’m thinking that might be related to my issue. I’m building a Shopify app that I want to host on Firebase. I’ve gotten it to authenticate and install, but when I try to capture the permanent access token via POST, Firebase times out. This same code works fine with ngrok. Entire route function below.

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);

3

Answers


  1. Chosen as BEST ANSWER

    Solved. Turns out you need a paid plan (Blaze, pay as you go) to access external APIs. I upgraded and that solved the issue.


  2. What is the request module that you are using for the request.post()
    Please see : https://www.npmjs.com/package/request#promises–asyncawait

    I hope you are using the https://github.com/request/request-promise module instead of request.

    Login or Signup to reply.
  3. You’re calling response.end() incorrectly:

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

    As you can see from the linked documentation, end() doesn’t take a parameter. It just ends the response. You probably want to be calling send() instead if you have data to send.

    If you’re unsure how your function is executing, also use console.log() to log messages to figure out exactly what it’s doing. It’s rarely a good idea to just hope that a bunch of code is just working – you should verify that it’s working the way you expect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search