[Fieldwire] Error 500 (internal error) from phase 3/3 of upload of attach [integration ERP - FieldWire via API]
Hi,
We are working on an integration between our ERP and fieldwire in c# .net using your api V3
We are stuck with the function to upload pdf file as attachment of a project (and the twin function, to upload pdf file in plans..).
In both cases phase 1 (request to fieldwire server) Is ok (no errors, we get the infos for next step); phase 2, upload files to S3 is also ok (no errors and we get the infos for next step), but when we try to execute the phase 3 (attachment), we always have an error in return (500 internal server error, which is not very useful to debug the causes of failure).
The strange thing is that these functions developed and tested in July, were working without problems (I still have the july logs with the positive results in execution for all the 3 phases).
We have also tried to test (again) the whole project recovered from backup of July with same pdf test files (and related successful logs) and the result is again phase 1 ok, phase 2 ok, phase 3 error 500 :
July tests OK with 2 pdf files :
05/07/2024 14:53:57 Lettura dati autorizzazione da fieldwire per caricare su S3 PHASE1
05/07/2024 14:53:57 Caricamento file in Amazon AWS S3: C:\Users\felice\source\repos2019\FieldWireWS\bin\Debug\Files\TestAF\TestAF.pdf PHASE2
05/07/2024 14:53:58 Link file in attachment su File fieldwire: C:\Users\felice\source\repos2019\FieldWireWS\bin\Debug\Files\TestAF\TestAF.pdf PHASE3
05/07/2024 14:53:58 Link effettuato : TestAF.pdf OK on PHASE3 (we saw the results in filedwire)
Same procedure for another pdf file :
05/07/2024 14:54:04 Lettura dati autorizzazione da fieldwire per caricare su S3 PHASE1
05/07/2024 14:54:04 Caricamento file in Amazon AWS S3: C:\Users\felice\source\repos2019\FieldWireWS\bin\Debug\Files\TestAF\TestAF2.pdf _PHASE2
05/07/2024 14:54:05 Link file in attachment su File fieldwire: C:\Users\felice\source\repos2019\FieldWireWS\bin\Debug\Files\TestAF\TestAF2.pdf _PHASE3
05/07/2024 14:54:05 Link effettuato : TestAF2.pdf OK on PHASE3
Last tests NOK made :
25/10/2024 17:34:56 Lettura dati autorizzazione da fieldwire per caricare su S3 PHASE1
25/10/2024 17:35:02 Caricamento file in Amazon AWS S3: C:\Users\felice\source\repos2019\FieldWireWS\bin\Debug\Files\TestAF\TestAF.pdf _PHASE2
25/10/2024 17:35:22 Link file in attachment su File fieldwire: C:\Users\felice\source\repos2019\FieldWireWS\bin\Debug\Files\TestAF\TestAF.pdf _PHASE3
25/10/2024 17:35:40 Response code: InternalServerError NOK on PHASE3
Response body: {"errors":["500 Internal Server Error"]}
ps: If we use the final url coming from S3 (+key+filename..) directly in a browser, we get an unauthorized access from S3, so in our opinion something has changed in phase 2 interaction with S3...
public static async Task<bool> UploadFileToS3andLinkFieldWire_Files(string tokenAccess, string projectId, string filePath, string userid) {
//Result of operation
bool Esito = false;
// 4 debug TestAF.pdf file
//filePath= "C:\\Users\\felice\\source\\repos\\FieldWire_WS\\bin\\Debug\\Files\\TestAF\\TestAF.pdf";
try
{
//Begin phase 1
LogElab.Logger("Lettura dati autorizzazione da fieldwire per caricare su S3");
var options = new RestClientOptions("https://client-api.us.fieldwire.com/api/v3/aws_post_tokens");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddHeader("accept", "application/json");
request.AddHeader("Fieldwire-Version", dataAPIFW);
request.AddHeader("authorization", "Bearer " + tokenAccess);
var awsPostTokenResponse = await client.PostAsync(request);
//Console.WriteLine("{0}", awsPostTokenResponse.Content);
/*
Example of Response (key altered for security reasons) :
{
"post_address": "https://s3.amazonaws.com/com.fieldwire.production.us-east-1.core.api.uploads",
"post_parameters": {
"key": "xxxxxxx_${filename}",
"x-amz-meta-original-filename": "${filename}",
"policy": "eyJleHBpcmF0aW9uIjoiMjAyNC0xMC0yNVQxMzoyMjo1OFoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJjb20uZmllbGR3aXJlLnByb2R1Y3Rpb24udXMtZWFzdC0xLmNvcmUuYXBpLnVwbG9hZHMifSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIjA2MGFmNzA5N2M5MjU4NDFlMGYxYjAyM2U0MjQxMjNhL2ZmMjE1OGQ4YTYzNGQ3MGFmNjNjNjk4NTNlOGI3MWU2XyJdLFsic3RhcnRzLXdpdGgiLCIkeC1hbXotbWV0YS1vcmlnaW5hbC1maWxlbmFtZSIsIiJdLHsieC1hbXotY3JlZGVudGlhbCI6IkFLSUFWVUpZWFBLNUpLRVRSS0ZVLzIwMjQxMDI0L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSx7IngtYW16LWFsZ29yaXRobSI6IkFXUzQtSE1BQy1TSEEyNTYifSx7IngtYW16LWRhdGUiOiIyMDI0MTAyNFQxMzIyNThaIn1dfQ==",
"x-amz-credential": "AKIAVUJYXPK5JKETRKFU/20241024/us-east-1/s3/aws4_request",
"x-amz-algorithm": "AWS4-HMAC-SHA256",
"x-amz-date": "20241024T132258Z",
"x-amz-signature": "159013b816d81d493859ad4172adfbec6a2a51bbb0492ec2bd22f1d03d6d9bd7"
}
}
*/
//End phase 1 No errors
//------------------------------------------------------------------------------------
//Begin phase 2
//Upload file in Amazon AWS S3
LogElab.Logger("Caricamento file in Amazon AWS S3: " + filePath);
var awsPostToken = JObject.Parse(awsPostTokenResponse.Content);
string s3PostAddress = awsPostToken["post_address"].ToString();
var s3PostParams = awsPostToken["post_parameters"].ToObject<Dictionary<string, string>>();
var clientS3 = new RestClient(s3PostAddress);
var requestS3 = new RestRequest("", Method.Post);
//Adding parameters
foreach (var param in s3PostParams)
{
requestS3.AddParameter(param.Key, param.Value);
}
//Adding file
requestS3.AddFile("file", filePath);
var s3PostResponse = await client.ExecuteAsync(request);
//End phase 2 no errors
//Begin Phase 3 attachment in fieldwire
if (s3PostResponse.IsSuccessful)
{
LogElab.Logger("Link file in attachment su File fieldwire: " + filePath);
//From S3 response
string s3Key = s3PostParams["key"].Replace("${filename}", Path.GetFileName(filePath));
string fileUrl = $"{s3PostAddress}/{s3Key}";
string fileName = Path.GetFileName(filePath);
//Link per attach Files in Fieldwire
var fieldwireClient = new RestClient($"https://client-api.us.fieldwire.com/api/v3/projects/{projectId}/attachments");
var fieldwireRequest = new RestRequest();
fieldwireRequest.Method = Method.Post; // usa Method.Post
fieldwireRequest.AddHeader("accept", "application/json");
fieldwireRequest.AddHeader("Fieldwire-Version", dataAPIFW);
fieldwireRequest.AddHeader("authorization", $"Bearer {tokenAccess}");
//long fileSize = new FileInfo(filePath).Length;
var jsonBody = JsonConvert.SerializeObject(new
{
creator_user_id = userid,
last_editor_user_id = userid,
name = fileName,
file_url = fileUrl,
// file_size=fileSize
});
fieldwireRequest.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
var fieldwireResponse = fieldwireClient.Execute(fieldwireRequest);
if (!fieldwireResponse.IsSuccessful)
{
//The response is not successful and we have always error 500 (internal error) from fieldwire server
LogElab.Logger($"Response code: {fieldwireResponse.StatusCode}\nResponse body: {fieldwireResponse.Content}");
throw new IOException($"Response code: {fieldwireResponse.StatusCode}\nResponse body: {fieldwireResponse.Content}");
}
else
{
LogElab.Logger("Link effettuato : " + fileName);
}
//End phase 3
Esito = true;
}
else
{
// Handle error responses according to AWS S3 documentation
// 403 (forbidden) means that you have modified the attributes parameters,
// 400 (bad request) can mean your token has expired, and/or your file is oversized
//return null;
LogElab.Logger("Errore su caricamento file su S3");
Esito = false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Errore: {ex.Message}");
LogElab.Logger($"Errore: {ex.Message}"); } return Esito; ```
}
Any suggestions? Something changed in S3 interaction?
Tanks in advance