LinkedIn使用RestClient共享API

本文关键字:API 共享 RestClient 使用 LinkedIn | 更新日期: 2023-09-27 18:21:45

我正试图使用OAuth v2将共享发布到LinkedIn-我已正确获得授权并拥有适当的访问密钥。

过帐到:https://api.linkedin.com/v1/people/~/股?oauth2_access_token=(访问令牌)

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

LinkedIn使用RestClient共享API

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

oRequest.AddBody(xml); 

当我用替换它时

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

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