APIs & Integrations

KSmithIllumine8
Contributor

Having trouble authenticating a post to upload an image to the file manager

I have a serverless function to upload an image to the file manager, but am currently getting a 401 error (the call is not properly authenticated).

 

I have it set up almost identically to a serverless function that pulls form submission data (which is working) and I've properly registered the secret and the function file (this is the same secret used for the form data pull, which has both forms and files scopes) so I'm not sure what I need to change about the authentication in order for HubSpot to accept it.

 

Here's my serverless function code for the file upload (not working, 401 error):

 

const axios = require('axios');

exports.main = async (context, sendResponse) => {

  const config = {
    headers: {
      Authorization: `Bearer ${process.env["my_secret_key"]}`,
      'Content-Type': 'image',
    },
    body: context,
  };

  try {
    // post image to file manager
    const response = await axios.post(`https://api.hubapi.com/files/v3/files/import-from-url/async`, config);

    sendResponse({ body: { title: "Image successfully uploaded!", description: "The call to upload the image was successful.", data: response }, statusCode: 200 });
  } catch (e) {
    sendResponse({ body: { message: e.message }, statusCode: 500 });
  }
};

 

 

 

And here's my serverless function code for the form data pull (working):

 

const axios = require('axios');

exports.main = async (context, sendResponse) => {

  const config = {
    headers: {
      Authorization: `Bearer ${process.env["my_secret_key"]}`,
      'Content-Type': 'application/json',
    },
  };

  try {
    // pull form submission data
    const response = await axios.get(`https://api.hubapi.com/form-integrations/v1/submissions/forms/{{form-id}}`, config);

    sendResponse({ body: { title: "form data successfully pulled!", description: "The call to the forms api to retrieve the submission data was successful.", data: response.data }, statusCode: 200 });
  } catch (e) {
    sendResponse({ body: { message: e.message }, statusCode: 500 });
  }
};

 

 

 

Thanks!

0 Upvotes
1 Reply 1
Jaycee_Lewis
Community Manager
Community Manager

Having trouble authenticating a post to upload an image to the file manager

Hey, @KSmithIllumine8 👋 Thanks for your question! 

 

I don't have the solution, but I have two questions for you:

  • have you tried using "Content-Type": "image/jpeg" or "image/png"(depending on the fie type you are using) instead of just "Content-Type": "image"?
  • have you tried logging the entire error object (console.error(e)) instead of just e.message? 

Talk soon! — Jaycee


Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success.
Don't miss this opportunity to connect and grow—reserve your spot today!


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes