如何使用RestSharp库在Linkedin中发布评论

本文关键字:布评论 评论 何使用 RestSharp 库在 Linkedin | 更新日期: 2023-09-27 18:25:24

在过去的一周里,我一直在努力在LinkedIn上添加帖子评论,但我还没有得到解决方案

protected void Page_Load(object sender, EventArgs e)
{
    string ConsumerKey = "**********";
    string ConsumerSecret = "**********";
    string Token = "**********";
    string TokenSecret = "**********";
    string xmlContent = "<?xml version='1.0' encoding='UTF-8'?>" + Environment.NewLine +
        "<comment>" + Environment.NewLine +
        "<text>Check out</text>" + Environment.NewLine +
        "</comment>";
    byte[] dataBytes = Encoding.Default.GetBytes(xmlContent);
    var postData = new Dictionary<string, string>()
    {
        {"key1","value1"}
    };
   String requestUrl = "http://api.linkedin.com/v1/posts/{post-id}/comments?oauth_token=" + Token;
    RestClient rc = new RestClient();
    RestRequest request = new RestRequest(requestUrl, Method.POST);
    request.AddHeader("Content-Type", "application/xml");
    request.AddHeader("x-li-format", "xml");
    request.RequestFormat = DataFormat.Xml;
    request.AddBody(xmlContent);
    RestResponse restResponse = (RestResponse)rc.Execute(request);
    ResponseStatus responseStatus = restResponse.ResponseStatus;
}

我收到一个错误

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
 <error>
     <status>401</status> 
     <timestamp>1375175962003</timestamp> 
     <request-id>10K340J5W6</request-id> 
     <error-code>0</error-code> 
     <message>[unauthorized]. ssl_required</message> 
 </error>

我想要解决方案。提前谢谢。

如何使用RestSharp库在Linkedin中发布评论

我发现有两件事不对。

  1. 错误表明您需要使用ssl(ssl_required),因此url应该是'字符串请求URL="https://api.linkedin...'而不是'字符串请求URL="http://api.linkedin...'

  2. restSharp.AddBody将对对象进行编码,我认为您需要使用

    request.AddParameter("text/xml", xmlContent, ParameterType.RequestBody); 
    

    而不是

     request.AddBody(xmlContent); 
    

    行。