我在 Wix 上有一个公开可用的博客,到目前为止,我已经通过 Wix 仪表板添加了我的所有帖子,但现在我想为此目的使用他们的 API。在帖子标题和内容的情况下一切正常,但我无法通过 API 向帖子添加图形。理论上,我是按照官方文档做的,我可能已经尝试了所有的可能性,包括下面介绍的方式,但是每次我浏览网站时,草稿中都有我定义的标题和内容,但是图像从来没有添加到它。有人知道解决方案吗?
# Set up the API endpoint and headers
url = "https://www.wixapis.com/blog/v3/draft-posts"
headers = {
"Authorization": "example_token_generated_from_wix_documentation_https://dev.wix.com/api/rest/authorization/oauth-2/request-an-access-token",
"Content-Type": "application/json"
}
# Set up the rich content of the blog post
rich_content = {
"nodes": [
{
"type": "PARAGRAPH",
"id": "",
"nodes": [
{
"type": "IMAGE",
"id": "",
"nodes": [],
"imageData": {
"type": "SITE",
"siteData": {
"url": f"https://static.wixstatic.com/media/image_id~mv2.jpg"
}
}
}
],
"paragraphData": {
"textStyle": {"textAlignment": "CENTER"},
"indentation": 0
}
},
{
"type": "PARAGRAPH",
"id": "",
"nodes": [
{
"type": "TEXT",
"id": "",
"nodes": [],
"textData": {
"text": "My blog post content goes here.",
"decorations": []
}
}
],
"paragraphData": {
"textStyle": {"textAlignment": "AUTO"},
"indentation": 0
}
}
]
}
# Set up the draft post data
data = {
"draftPost": {
"title": "My Blog Post Title",
"richContent": rich_content,
"heroImage": {
"type": "SITE",
"siteData": {
"url": "https://static.wixstatic.com/media/123~mv2.jpg"
}
}
}
}
# Make the API call
response = requests.post(url, headers=headers, data=json.dumps(data))
更新 1 我认为问题可能是由于无法访问适当的权限,这就是为什么只有文本在草稿帖子中可见。不幸的是,即使我直接从文件中加载图像,即使正确添加了草稿并返回了状态 200,图形仍然不可见。
# Read the image data from disk and encode it in base64
with open('image.png', 'rb') as f:
image_data = base64.b64encode(f.read()).decode()
# Set up the rich content of the blog post
rich_content = {
"nodes": [
{
"type": "PARAGRAPH",
"id": "",
"nodes": [
{
"type": "TEXT",
"id": "",
"nodes": [],
"textData": {
"text": "My example blog post content 1.",
"decorations": []
}
}
]
},
{
"type": "IMAGE",
"id": "",
"nodes": [
{
"imageData": {
"image": {
"src": {
"base64": image_data,
"altText": "Example image"
}
}
}
}
]
},
{
"type": "PARAGRAPH",
"id": "",
"nodes": [
{
"type": "TEXT",
"id": "",
"nodes": [],
"textData": {
"text": "My example blog post content 2.",
"decorations": []
}
}
]
},
{
"type": "IMAGE",
"id": "",
"nodes": [
{
"imageData": {
"image": {
"src": {
"url": "https://static.wixstatic.com/media/image_id~mv2.jpg",
"altText": "Example image",
"private": True
}
}
}
}
]
},
]
}
# Set up the draft post data
data = {
"draftPost": {
"title": "My Blog Post Title",
"richContent": rich_content
}
}
# Make the API call
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.status_code)
# Check the response status code
if response.status_code == 200:
print("Blog post created successfully!")
print(response.text)
else:
print("Failed to create blog post: " + str(response.text))
更新 2 我注意到,当我如下所示定义一个节点时,我的帖子草稿中似乎有一个图像对象,但它根本不可见,看起来像屏幕截图所示。我已经尝试从 Wix 媒体管理器和外部站点添加链接,并且我确保在打开链接时图像可用,但不幸的是每次结果看起来都一样。有什么想法吗?
{
"type": "IMAGE",
"id": "",
"nodes": [
{
"type": "IMAGE",
"id": "",
"nodes": [],
"imageData": {
"image": {
"src": {
"url": "https://static.wixstatic.com/media/2d7149_6b51eeb779cd460099e98e7ce2f14e1c~mv2.jpg"
},
"height": 1920,
"width": 1080,
},
}
}
]
}