-
Notifications
You must be signed in to change notification settings - Fork 707
Closed
Labels
Description
import os
import requests
from typing import Dict
PINTEREST_ACCESS_TOKEN = os.getenv("PINTEREST_ACCESS_TOKEN")
PINTEREST_BOARD_ID = os.getenv("PINTEREST_BOARD_ID")
PINTEREST_PINS_URL = "https://api.pinterest.com/v5/pins"
def create_pin(image_url: str, title: str, description: str, alt_text: str) -> Dict:
"""
Create a Pinterest pin using the v5 API.
"""
if not PINTEREST_ACCESS_TOKEN:
raise RuntimeError("PINTEREST_ACCESS_TOKEN environment variable is not set")
if not PINTEREST_BOARD_ID:
raise RuntimeError("PINTEREST_BOARD_ID environment variable is not set")
headers = {
"Authorization": f"Bearer {PINTEREST_ACCESS_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"board_id": PINTEREST_BOARD_ID,
"title": title[:100],
"description": description[:500],
"alt_text": alt_text[:500],
"media_source": {
"source_type": "image_url",
"url": image_url
}
}
response = requests.post(
PINTEREST_PINS_URL,
headers=headers,
json=payload,
timeout=30
)
response.raise_for_status()
return response.json()