LinkedIn使用RestClient共享API

时间:2013-07-10 14:41:26

标签: c# xml linkedin linkedin-api rest-client

我正在尝试使用OAuth v2向LinkedIn发布分享 - 我已正确获得授权并拥有相应的访问密钥。

发布到:https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=(access令牌)

分享XML:

> <?xml version="1.0" encoding="UTF-8"?> <share> <content> <title>White
> Cobalt Ltd</title> <description>Test</description>
> <submitted-url>https://www.whitecobalt.com/</submitted-url> </content>
> <visibility> <code>anyone</code> </visibility> </share>

发布代码(C#):

RestClient oRC = new RestClient();
RestRequest oRequest = new RestRequest(sURL, Method.POST);
oRequest.AddHeader("Content-Type", "application/xml");
oRequest.AddHeader("x-li-format", "xml");
oRequest.RequestFormat = DataFormat.Xml;
oRequest.AddBody(xml);

RestResponse oResponse = (RestResponse)oRC.Execute(oRequest);
if ((oResponse.ResponseStatus == ResponseStatus.Completed) && (oResponse.StatusCode == HttpStatusCode.Accepted))
{
... success
}

当我使用API​​控制台(https://apigee.com/console/linkedin)时,它工作得很好,但是当我尝试通过代码运行它时(据我所知,它做的完全相同)我得到以下错误:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
<status>400</status>
<timestamp>1373465292361</timestamp>
<request-id>FQEJFQOL6U</request-id>
<error-code>0</error-code>
<message>Couldn't parse share document: error: The document is not a share@http://api.linkedin.com/v1: document element local name mismatch expected share got String</message>
</error>

1 个答案:

答案 0 :(得分:0)

我相信我找到了解决方案 - 由于某种原因,LinkedIn API不喜欢RestClient功能:

oRequest.AddBody(xml); 

当我用以下代替时:

oRequest.AddParameter("application/xml", sbXML.ToString(), ParameterType.RequestBody);

代码开始正常工作 - 必须以稍微不同的方式添加Post数据。