401 error trying to download PDF using axios

This topic was automatically generated from Slack. You can find the original thread here.

Neil Lovelace : Good Morning Everyone,
I have another issue that I can’t seem to figure out. I’m needing to download a PDF from my e-signature platform PandaDoc via Axios; to do this I must use authentication, which I am doing. It works in PostMan, but I’m missing something in PipeDream.

In my code, I am calling this async function using await download(...) which is supposed to write to the /tmp directory but am continuing to get a 401 error and the message indicating that I must await all promises, which is puzzling as I am using async/await function as well as await Axios to download the file. Any suggestions are welcomed.

async function download(token, doc_id, fileName){
  const AXIOS = require("axios");
  const FS = require("fs");

  // SET THE FILE PATH FOR TEMP STORAGE
  const filePath = `/tmp/${fileName}`;

   // DOWNLOAD ATTACHMENT
  const response = await AXIOS({
    url: `${PD_BASEURL}/public/v1/documents/${doc_id}/download`,
    method: 'GET',
    responseType: 'stream',
    headers: AXIOS_HDR(token)});
  }

  //WRITE THE FILE TO TEMP STORAGE
  response.data.pipe(FS.createWriteStream(filePath));

  return new Promise((resolve, reject) => {
          response.data.on('end', () =>{
            resolve();
          });
          response.data.on('error', err => {
            reject(err);
          });
        });
  
  console.log(`File Saved To /tmp`);

Giao Phan : May not be an answer to your problem, but I’ve found using nodejs stream.pipeline is less error prone than trying to handle the callbacks yourself: Stream | Node.js v10.24.0 Documentation

Dylan Sather (Pipedream) : what’s the value of AXIOS_HDR(token)? No need to paste the access token, but is that a string, an object, etc.?

Dylan Sather (Pipedream) : the 401 error suggests that’s the token either isn’t getting passed correctly, or the access token could be expired. How are you generating that access token? Could it be expired?

Neil Lovelace : AXIOS_HDR(token) is a function to create the header value for the API call.

Neil Lovelace :

Neil Lovelace :

Neil Lovelace :

Neil Lovelace : Their tokens expire after 1 year. It’s active.

SERW2039 : yeah for me it sounds like it is needed to await the function that gets the token

Dylan Sather (Pipedream) : can you share your workflow with dylan@pipedream.com?

Neil Lovelace : It currently is shared with you. It’s called PandaDoc Listener.

Dylan Sather (Pipedream) : Can I make a small modification to that step and run the workflow as a test?

Neil Lovelace : Feel free.
I just noticed that maybe I need to place the function that builds the header inside of the download function. Perhaps it is a scope thing after all.

Dylan Sather (Pipedream) : also I see it’s failing at an earlier step now - were you testing something else?

Neil Lovelace : My Checkpoint step is turned off at the moment. It ends the workflow when no metadata is returned… which is all of those errors stating that it can’t read property substring from undefined.

Dylan Sather (Pipedream) : are you able to get the workflow back to the state where you were encountering the 401s? I’d like to make a small change just to enable more logging in the HTTP request, which should help us identify the core issue

Neil Lovelace : Sure thing.

Neil Lovelace : Its back to ‘normal’

Dylan Sather (Pipedream) : would you mind triggering a new request? I made the change but I’m hitting your checkpoint logic trying to process a previous event