I have a social media app, and when a user wants to upload a profile picture, they must obtain a signature from the server. This code works when I remove the upload preset in the front end, but I want the upload preset so that the user can upload to a specific folder, and it is a signed upload preset, so why am I getting 401 unauthorized error?
on the backend
const generateSignature = async (req, res) => {
/* It's just getting the current time in seconds. */
const timestamp = Math.round(new Date().getTime() / 1000);
try {
const signature = cloudinary.utils.api_sign_request(
{
timestamp,
},
cloudinaryConfig.api_secret
);
res.status(200).json({
success: true,
timestamp,
signature,
api_key: cloudinaryConfig.api_key,
cloud_name: cloudinaryConfig.cloud_name,
});
} catch (err) {
console.log(err);
res.status(500).json({ success: false, message: "server error try again" });
}
};
on the frontend
const { timestamp, signature, api_key, cloud_name } =
signatureResponse.data;
const formData = new FormData();
formData.append("file", image);
formData.append("upload_preset", "uploadProfilePicture");// if i remove this line it works
formData.append("api_key", api_key);
formData.append("cloud_name", cloud_name);
formData.append("signature", signature);
formData.append("timestamp", timestamp);
console.log(formData);
const cloudinaryResponse = await axios.post(
`https://api.cloudinary.com/v1_1/${cloud_name}/image/upload`,
formData
);