我正在尝试将几张图像从我的电脑上传到Figma 文件。该脚本可以看到文件、其节点、id,但无法将图像放入文件中,尽管没有错误。我已经通过 python -m http.server 8000 创建了服务器,并从那里上传。 如果有人有解决此问题的想法,请写信或询问详细信息。
代码如下:
import requests
import os
import traceback
# Personal Figma token
TOKEN = "token"
FILE_KEY = "file key"
# Auth
HEADERS = {
"X-Figma-Token": TOKEN
}
# Uploading to Figma
def upload_image_to_node(node_id, image_url):
url = f"https://api.figma.com/v1/images/{FILE_KEY}"
json_data = {
"requests": [
{
"node_id": node_id,
"fill": {
"type": "IMAGE",
"image_url": image_url,
"scale_mod": "FILL"
}
}
]
}
response = requests.post(url, headers=HEADERS, json=json_data)
if response.status_code == 200:
print(f"Image {image_url} successfully uploaded to node {node_id}.")
else:
print(f"Error: {response.status_code}, {response.text}")
# Main
def process_images(input_folder, node_id):
base_url = "http://localhost:8000"
for filename in os.listdir(input_folder):
if filename.endswith((".png", ".jpg")):
image_url = f"{base_url}/{filename}"
upload_image_to_node(node_id, image_url)
try:
node_id = "0:1" # Main node in Figma
process_images("out/", node_id) # out/ is where images stored
except Exception as e:
print("Error occured:")
traceback.print_exc()
input("Press Enter to quit..")
Figma 的 REST API 无法实现这一点。没有端点允许这样做。另请注意,您使用的端点仅支持 GET 方法。请参阅开发者文档:https://www.figma.com/developers/api#files-endpoints