通过LinkedIn API编辑共享内容

时间:2019-06-19 08:21:59

标签: linkedin linkedin-api

作为LinkedIn Campaign Manager的用户,我可以在用户界面中更新以下内容:

  • 广告名称
  • 介绍性文字
  • 目标网址,

在共享中:

  • 广告图片,
  • 标题,
  • 说明

根据您的文档you can only edit the text of the ad。使用您的沙盒帐户并尝试上面链接中的示例请求,我得到一个非常奇怪的错误:

"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."

关于我找不到任何信息。 它允许我发布共享,但不能编辑它们。

另一个问题是,LinkedIn API不会返回草稿广告。也可以下载/更新草稿吗?

该服务正在ASP.NET MVC5上运行。

代码:

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;
}

型号:

    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; }
}

控制器:

    /// <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配置:

<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>

0 个答案:

没有答案
相关问题