如何使用RestSharp库在Linkedin上发表评论?

时间:2013-07-30 09:22:26

标签: c# linkedin restsharp

在过去的一周里,我很难在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>

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

1 个答案:

答案 0 :(得分:0)

我可以看到两件事情错了。

  1. 错误说你需要使用ssl(ssl_required)所以网址应该是 'String requestUrl =“https://api.linkedin ......” 并不是 'String requestUrl =“http://api.linkedin ...”

  2. restSharp.AddBody将对象进行编码,相信你需要使用

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

    而不是

     request.AddBody(xmlContent); 
    

    线。