As a user of LinkedIn Campaign Manager I'm able to update the following in the UI:
- Name of the ad,
- Introductory text,
- Destination URL,
And within the share:
- Ad image,
- Headline,
- Description
According to your docs, you can only edit the text of the ad. Using your sandbox account and trying the sample request from the link above I'm getting a very strange looking error:
"com.linkedin.restli.client.RestLiResponseException: Response status 400, serviceErrorMessage: com.linkedin.publishing.util.common.ResponseException: Editing of properties other than and WLed parameters is not allowed."
About which I'm not able to find any information. It allows me to post shares, but not edit them.
Another issue is that LinkedIn API doesn't return Draft Creatives. Is there any way to download/update drafts too?
The service is running on ASP.NET MVC5.
Code:
public class EditASingleShareService : IEditASingleShareService
{
public async Task<string> EditASingleSharesTextAsync(ShareEditUserView share, long share_id)
{
ShareEditRequest shareEditRequestModel = InitializeTheShareRequestObject(share);
using (var httpClient = new HttpClient())
{
StaticMethods.AuthorizeToken(httpClient);
var reqUrl = $"{ConfigurationManager.AppSettings["ShareEndPoint"]}/urn:li:share:{share_id}";
ByteArrayContent byteContent = StaticMethods.CreateByteArrayContent(shareEditRequestModel);
var response = await httpClient.PostAsync(reqUrl, byteContent);
response.EnsureSuccessStatusCode();
return $"The correct status code is 204. Actual response: {response.ToString()}";
}
}
private static ShareEditRequest InitializeTheShareRequestObject(ShareEditUserView share)
{
var shareEditRequestModel = new ShareEditRequest()
{
patch = new Patch()
{
set = new Set()
{
text = new Text()
{
annotations = new List<object>()
}
}
}
};
shareEditRequestModel.patch.set.text.text = share.Text;
return shareEditRequestModel;
}
}
public static void AuthorizeToken(HttpClient httpClient)
{
string accessTkn = ConfigurationManager.AppSettings["AuthToken"];
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessTkn);
}
public static ByteArrayContent CreateByteArrayContent(object anyTypeofAdObject)
{
var myContent = JsonConvert.SerializeObject(anyTypeofAdObject);
var buffer = Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return byteContent;
}
Models:
public class ShareEditRequest
{
public Patch patch { get; set; }
}
public class Text
{
public List<object> annotations { get; set; }
public string text { get; set; }
}
public class Set
{
public Text text { get; set; }
}
public class Patch
{
[JsonProperty("$set")]
public Set set { get; set; }
}
}
public class ShareEditUserView
{
public string Text { get; set; }
}
Controller:
/// <summary>
/// Edit a single shares text (only editable field).
/// </summary>
/// <param name="share">Share user view.</param>
/// <param name="share_id">ID of the share</param>
/// <returns>Response code</returns>
[HttpPost]
public async Task<string> EditASingleShareAsync(ShareEditUserView share, long share_id)
{
return await editShare.EditASingleSharesTextAsync(share, share_id);
}
Web Config:
<appSettings>
<add key="CampaignEndPoint" value="https://api.linkedin.com/v2/adCampaignsV2" />
<add key="CreativeEndPoint" value="https://api.linkedin.com/v2/adCreativesV2" />
<add key="ShareEndPoint" value="https://api.linkedin.com/v2/shares" />
</appSettings>