LinkedIn API 透過 HTTP 要求節點建立貼文出現錯誤,文字格式問題

描述問題/錯誤/問題

我正在使用 AI 節點生成一些文字來[發佈到我的 LinkedIn,我已經使用 HTTP 請求 GET 測試了憑據,所以我知道那是有效的。
當試圖為 AI 生成的文字建立貼文時,我不斷收到錯誤,例如:
400 - “{"status":400,"code":"ILLEGAL_ARGUMENT","message":"Request body could not be converted to data map"}”
完整訊息
422 - “{"message":"ERROR :: /## Audit Log Ingestion Lag\n\nImagine you’re trying to catch a thief who just left a store :: unrecognized field found but not allowed\nERROR :: /author :: field is required but not found and has no default value\nERROR :: /commentary :: field is required but not found and has no default value\nERROR :: /visibility :: field is required but not found and has no default value\nERROR :: /distribution :: field is required but not found and has no default value\nERROR :: /lifecycleState :: field is required but not found and has no default value\n","status":422}”

請分享你的工作流程

無法複製節點,似乎無法複製文字(Windows 11 上的 Edge)
AI 節點運作正常,只是透過 HTTP 請求發佈 LinkedIn 貼文無法發佈

分享最後一個節點返回的輸出

422 - “{"message":"ERROR :: /## Audit Log Ingestion Lag\n\nImagine you’re trying to catch a thief who just left a store :: unrecognized field found but not allowed\nERROR :: /author :: field is required but not found and has no default value\nERROR :: /commentary :: field is required but not found and has no default value\nERROR :: /visibility :: field is required but not found and has no default value\nERROR :: /distribution :: field is required but not found and has no default value\nERROR :: /lifecycleState :: field is required but not found and has no default value\n","status":422}”

關於你的 n8n 設定的資訊

  • n8n 版本:2.30.0
  • 資料庫(預設:SQLite):預設
  • n8n EXECUTIONS_PROCESS 設定(預設:own, main):預設
  • 執行 n8n 的方式(Docker、npm、n8n cloud、桌面應用程式):Proxmox lxc
  • 作業系統:Proxmox

感謝你告知我們這一點,我們已建立 CV-9 作為內部開發票證來進行調查。

好的,我已經設法解決了這些問題,原來是LinkedIn API發佈功能要求在貼文本文中添加某些特定欄位。

我還發現通過API發佈時有4000個字元的限制。

這兩個錯誤都指向相同的兩個根本原因,而且都可以修復:

  1. 400「Request body could not be converted to data map」表示 JSON 本體本身無效——你的 AI 文本被注入為原始格式,所以它的引號、換行符和 :# 等字符會破壞 JSON。修復方法:將 HTTP Request body 設定為 JSON 模式,並用 JSON.stringify 包裝文本以便進行轉義,例如 commentary 值變為 ={{ JSON.stringify($json.text) }}(在模板中刪除外層引號)。這樣就能解決「converted to data map」錯誤。

  2. 422「field is required」列表(author、commentary、visibility、distribution、lifecycleState)表示你發送的是較新的 /rest/posts 端點,但主體不完整。該端點需要所有這些欄位。最小可用主體:

{
“author”: “urn:li:person:XXXX”, // 或 urn:li:organization:XXXX
“commentary”: “your text here”,
“visibility”: “PUBLIC”,
“distribution”: {
“feedDistribution”: “MAIN_FEED”,
“targetEntities”: ,
“thirdPartyDistributionChannels”:
},
“lifecycleState”: “PUBLISHED”,
“isReshareDisabledByAuthor”: false
}

還有兩個標頭人們通常會遺漏:LinkedIn-Version: 202401(使用當前 YYYYMM)和 X-Restli-Protocol-Version: 2.0.0。沒有版本標頭,LinkedIn 會靜默拒絕架構。

/rest/posts commentary 欄位特有的另一個陷阱:它使用 LinkedIn 的「Little Text」格式,所以字面上的 () ó òó@#* 及其他一些字符必須用反斜線轉義,否則請求會返回 422。如果你的 AI 文本包含 markdown 標題(##)或括號,在發送前應該刪除或轉義它們。你的示例文本包含 ##::,這幾乎肯定是導致問題的原因。

如果你貼上確切的節點組態(端點 URL + 你發送的欄位),我可以指出具體缺少的部分。

我們已檢視此問題,但無法確認這是否為 bug。目前我們已關閉內部工單,但如果看起來確實是 bug,我們的審核團隊將再次標記。

好找!對於遇到相同問題的人,以下是 LinkedIn 的 ugcPosts 端點所需的最小請求正文:

{
  "author": "urn:li:person:YOUR_PERSON_ID",
  "lifecycleState": "PUBLISHED",
  "specificContent": {
    "com.linkedin.ugc.ShareContent": {
      "shareCommentary": {
        "text": "Your post text here"
      },
      "shareMediaCategory": "NONE"
    }
  },
  "visibility": {
    "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
  }
}

另外值得注意的是:4000 個字元的限制適用於 text 欄位。如果你使用 AI 來生成內容,請在發送前添加長度檢查——否則 API 只會默默地拒絕請求。