I'm having some issues with posting creatives through LinkedIn API.
That's a question purely for LinkedIn engineers, as I'm sure it's not the code issue. The problem seems to be happening during bulk-posting Campaigns with associated Creatives. Using Thread.Sleep
/ await Task.Delay
methods and Synchronous run do not help. The error is not descriptive at all, but I have crossed off the bad request and code error reasons. It's definitely an API problem. I'd like to have more insights on the number of calls I can make to the server and how often can I make those, but it doesn't seem to be related to that either. I tried debugging and waiting 10-20 seconds before posting a Creative.
What I'm doing through the API is below:
Let me add, that I have made sure that by the time I'm trying to post a Creative it does have both referenced shares' ID and referenced campaigns' ID.
The error message:
{"message":"Validation failed because [{reason=FAIL_TO_RETRIEVE_UPDATE, field=reference, batchIndex=0, type=REMOTE_OPERATION_FAILED, message=Fail to retrieve update. Could be a temporary failure. Reference , parameters={value=, key=}}]","status":400}
In the meantime, please have a look at the following work-around this issue:
I've introduced some retry logic coupled with await Task.Delay()
method, which seems to be helping a little and if it fails with the above error, 4-5 retries usually is enough to proceed.
public async Task<string> PostTheContentAsync(HttpClient httpClient, object campaignManagerObject, string reqUrl, int retriesRemaining = 5)
{
try
{
ByteArrayContent byteContent = CreateByteArrayContent(campaignManagerObject);
var response = await httpClient.PostAsync(reqUrl, byteContent);
response.EnsureSuccessStatusCode();
IEnumerable<string> headerValue = response.Headers.GetValues("X-LinkedIn-Id");
return headerValue.First();
}
catch
{
if (retriesRemaining > 0) //and exception is temporary
{
await Task.Delay(5000);
return await PostTheContentAsync(httpClient, campaignManagerObject, reqUrl, retriesRemaining - 1);
}
throw new Exception($"Error while posting the content of {campaignManagerObject.GetType()} for URL: {reqUrl} in {nameof(ManagementBaseClass)}, {nameof(PostTheContentAsync)}.");
}
}