'puppeteer page navigation issues with heroku deployment

I am using TD Ameritrade's API and am trying to implement an automated work flow for the access token refresh process. I have chosen to use the npm package "puppeteer" to programmatically login to the TD Ameritrade developer website and retrieve my code(a long string that needs to be URL decoded needed for the access token refresh process) from a URI that the bot gets redirected to after logging in and accepting the terms for further permissions to access my trading account.

Everything worked fine on my end, but when I deployed my code to heroku the redirect to the URI does not happen and I am only able to grab the URL of the site I was on before the redirect to the URI.

Here is my code:

const getTdAmeritradeCode = async () => {
    const browser = await puppeteer.launch({
        headless: true,
        args: ['--no-sandbox','--disable-setuid-sandbox']
    });

    const page = await browser.newPage();
    await page.goto(process.env.REFRESH_TOKEN_REFRESH_LINK);
    await page.type("#username0", process.env.TD_AMERITRADE_USERNAME);
    await page.type("#password1", process.env.TD_AMERITRADE_PASSWORD);
    const [response0] = await Promise.all([
        page.waitForNavigation(),
        page.click("#accept"),
    ]);
    const [response1] = await Promise.all([
        page.waitForNavigation(),
        page.click("#accept"),
    ]);

    let tdAmeritradeCode = decode(await page.url().slice(50)); // 50 is the number of characters in the url before the code
    await browser.close();
    return tdAmeritradeCode;
}

That is the method I have found on the puppeteer documentation, but it does not seem to be working. Like I said earlier, the code works as intended on my machine but when deployed on heroku the code fails the response1 Promise and does not get redirected to the URI.



Solution 1:[1]

The issue was I had my TD Ameritrade application redirect to my Heroku application's URL. I believe it's due to the fact that Heroku applications are not meant to make a GET request to themselves. I ended up changing the redirect URI in my TD Ameritrade and Heroku applications and I was able to get the code without problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Moon