错误 400:通过 LinkedIn 分享发帖时请求错误 API

Error 400: Bad Request while posting through LinkedIn Share API

我在 Whosebug 上查了一堆问题,这些问题可能被认为是相关的,但大多数问题要么太旧要么太具体,对我帮助不大。

在开发有助于在 LinkedIn 上安排 post 的应用程序时,我遵循以下过程:

  1. 允许用户使用他们的 LinkedIn 帐户注册。
  2. 他们提交注册后,我会存储 Access TokenRefresh Token 和相关 TTL 以及用户详细信息。
  3. 当他们在特定时间安排 post 时,我有一个 Azure 函数(无服务器)每分钟检查是否有任何 post 提交,并且应该 post代表用户。
  4. 使用我之前保存的Access Token,我点击https://api.linkedin.com/v2/me来获取用户的person:id。一切都按预期进行,直到这里并使用 GetPersonID()
  5. 获得 person:id

使用 API:

post 查看我下面的代码
      string pid = GetPersonID();
      sstring PostCode = @"{""content"":{""contentEntities"":[{""entityLocation"":""" + URL + @""",""thumbnails"":[{""imageSpecificContent"":{},""resolvedUrl"":""" + OriginalURL + @"""}]}],""description"":"" + URL + "",""title"":""" + Title + @"""},""distribution"":{""linkedInDistributionTarget"":{}},""owner"":""urn:li:person:" + pid + @""",""subject"":""" + Title + @""",""text"":{""text"":""" + Description + @"""}}";
      string outJ = JsonConvert.SerializeObject(PostCode);
      WebClient clientx = new WebClient();
      clientx.Headers.Add("Authorization", "Bearer " + access_token);
      clientx.Headers.Add("X-Restli-Protocol-Version", "2.0.0");
      clientx.Headers.Add("x-li-format", "json");
      clientx.Headers["Content-Type"] = "application/json";
      System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
      string LinkedInAccessTokenURL = "https://api.linkedin.com/v2/shares";
      string result = clientx.UploadString(LinkedInAccessTokenURL, "POST", data: outJ);

一个样本 Json Post

    {"content":{"contentEntities":[{"entityLocation":"http://www.test.com","thumbnails":[{"imageSpecificContent":{},"resolvedUrl":"https://picsum.photos/200/300"}]}],"description":" + URL + ","title":"This is a test post"},"distribution":{"linkedInDistributionTarget":{}},"owner":"urn:li:person:<person:id>","subject":"This is a test post for LinkedIn API","text":{"text":"ashdjahs dadjh asdjahs da\nahs djashdjkashdas\nahsd jasdhkjasdh "}}

上述代码的最后一行之前一切正常。 POST 时,LinkedIn API 抛出错误 System.Net.WebException: 'The remote server returned an error: (400) Bad Request.' (LinkedIn documentation reference)。我什至尝试过使用 Postman posting PostCode 中的值,它工作得很好,这告诉我 JSON 生成它是正确的。

但是尽管进行了多次尝试和修复,但对于实际 Post

似乎没有任何效果

抛出异常:

Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: null
InnerException: null
Message: "The remote server returned an error: (400) Bad Request."
Response: {System.Net.HttpWebResponse}
Source: "System.Net.Requests"
StackTrace: "   at System.Net.HttpWebRequest.GetResponse()\r\n   at System.Net.WebClient.GetWebResponse(WebRequest request)\r\n   at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream)\r\n   at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer)\r\n   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)\r\n   at System.Net.WebClient.UploadString(Uri address, String method, String data)\r\n   at System.Net.WebClient.UploadString(String address, String method, String data)"
Status: ProtocolError
TargetSite: {System.Net.WebResponse GetResponse()}

标记为已解决。

问题显然与正在生成的 Json 有关。可能是编码问题,但我仍然对 Postman 如何 post 它没有任何问题感到困惑。

我创建了相关的 class,然后使用它的对象,我使用 Newtonsoft 将它序列化为 Json。在传递给 WebClient 对象后呈现正确的结果。

吸取教训!

我知道手动创建一个 JSON 从来都不是一个好主意(我之前实际上是这样做的,以节省时间),但有时懒惰会让你比最初更努力地工作.因此,如果我们计划使用 JSON 作为有效负载进行 API 调用,那么创建相关 class 然后序列化为 Json 绝不会浪费时间。 =11=]