LinkedIn Post API:如果帖子文本包含“()”,则会被截断

问题描述 投票:0回答:4

我正在尝试通过他们的 API 在 LinkedIn 上发帖。 端点

https://api.linkedin.com/rest/posts

集成工作正常,直到我尝试发布带有

(
字符的内容。重新编辑的示例:

  {
  author: '',
  commentary: 'a line\n' +
    '\n' +
    'another line...\n' +
    '\n' +
    '\n' +
    'This gets posted (it will get cut off from here)\n' +
    'another line\n'
  ...
}

如果尝试对字符串进行 url 编码以及 JSON.stringify 整个对象。 第一个发布的所有转义字符仍然存在(即:

a%20line%20
),并且也会被切断,因为encodeURI不会转义
()
。 后者会在 API 中抛出错误,提示“注释字符串格式错误”。

有什么想法可以解决这个问题吗?

编辑:

这里提到了最底部的转义:https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/little-text-format?view=li-lms- 2022-08#文字

但是:如果我这样做

\\(
API 将返回
"The input string format is invalid"

javascript node.js axios linkedin-api
4个回答
6
投票

好吧,所以问题确实是一些奇怪的逃逸东西。

通过在字符串上运行

text.replace(/[\(*\)\[\]\{\}<>@|~_]/gm, (x) => "\\" + x)
来解决。


5
投票

我也遇到了同样的问题,这是我在 PHP 中修复它的方法:

$sanitizedDescription = preg_replace_callback('/([\(\)\{\}\[\]])|([@*<>\\\\\_~])/m', function ($matches) {
    return '\\'.$matches[0];
    }, $descriptionToSanitize);

上面的代码片段的作用是匹配正则表达式中提到的每个特殊字符,并将它们替换为转义字符,以便 LinkedIn 将它们视为文字。


0
投票

以防万一有人正在寻找 Python 解决方案。


def escape_text(text):
    chars = [",", "{", "}", "@", "[", "]", "(", ")", "<", ">", "#", "*", "_", "~"]
    for char in chars:
        text = text.replace(char, "\\"+char)
    return text


0
投票

你可以使用 \ 来转义 (

示例:

This gets posted \(it will not get cut off from here\)

© www.soinside.com 2019 - 2024. All rights reserved.