Facebook API 端点"/{GROUP_ID}/feed"的字符问题



下面的代码可以正常工作,但是在Facebook中一旦出现"&">

TOKEN_ID = '<Token>'
GROUP_ID = '<group_id>'
headers   = { 'Content-Type': 'text/html; charset=UTF-8;', }
msg = 'Automation test & Python script'
fb_url = f"https://graph.facebook.com/v13.0/{GROUP_ID}/feed?message={msg}&access_token={TOKEN_ID}"
resp = requests.post(fb_url, headers=headers)
pprint(resp.json())

return:'id': '686578895356485_972982316716140'}

我想发布Automation test & Python script和帖子只有Automation test,你可以看到与捕获问题

这似乎是一个编码问题,但我不知道如何解决它。

谢谢你的帮助。

我找到了一个有效的解决方案。如我们所料,这是一个编码问题。

import urllib.parse

TOKEN_ID = '<Token>'
GROUP_ID = '<group_id>'
headers   = { 'Content-Type': 'text/html; charset=UTF-8;', }
msg = 'Automation test & Python script'
msg = urllib.parse.quote_plus(msg)
fb_url = f"https://graph.facebook.com/v13.0/{GROUP_ID}/feed?message={msg}&access_token={TOKEN_ID}"
resp = requests.post(fb_url, headers=headers)
pprint(resp.json())

如果你有更好或更干净的,我就买了。

最新更新