0
votes

I can't seem to generate the correct signature to upload an image successfully.

Error message -

message:'Invalid Signature 953e29c9b5cac0f611e347d508aaff75f11f0547. String to sign - 'timestamp=1631318071'.'

Maybe someone can tell me what I'm going wrong? Maybe the order of something (according to the docs)?

FYI - I tried changing the order I pass in the parameters server side but it didn't seem to do much to successfully upload.

Here is how I generate the signature client side (C#) and pass data to the client (Angular)

DateTime foo = DateTime.Now;
long timeStamp = ((DateTimeOffset)foo).ToUnixTimeSeconds();
var p = new Dictionary<string, object>();
p.Add("api_key", _cloudinaryConfig.Value.ApiKey);
p.Add("api_secret", _cloudinaryConfig.Value.ApiSecret);
p.Add("cloud_name", _cloudinaryConfig.Value.CloudName);
p.Add("timestamp", timeStamp);
var signature = _cloudinary.Api.SignParameters(p);
return Ok(new {
   cloudUploadUrl = _cloudUploadUrl, 
   cloudName = _cloudinaryConfig.Value.CloudName, 
   cloudApiKey = _cloudinaryConfig.Value.ApiKey, 
   cloudSignature = signature,
   cloudTimeStamp = timeStamp
});

Here is the client trying to upload an image in Angular

const data = new FormData();
data.append('file', croppedImage.base64);
data.append("api_key", cloudinaryParams.cloudApiKey);
data.append("signature", cloudinaryParams.cloudSignature);
data.append("timestamp", cloudinaryParams.cloudTimeStamp);

const fileResult = await fetch(cloudinaryParams.cloudUploadUrl, {
  method: 'POST',
  body: data,
});

const fileData = await fileResult.json();

if (fileData.error) {
  // do something to handle the error!
}
1

1 Answers

1
votes

The string in the error message shows the parameters that are being checked on the server side against the signature you provided on the upload API call.

Based on that error, the only parameter that should be used to create the signature for your example upload API call is "timestamp", and it needs to have a value of 1631318071 on both the API call and in the function that created the signature: https://cloudinary.com/documentation/signatures

If I understand your C# code correctly, you can most likely fix this by removing all the p.add() calls except the one with the timestamp parameter