Facebook Graph API Skill (Advanced)
Purpose
Production-oriented guide for building Facebook Graph API workflows for Pages:
publishing posts (text + image), managing tokens, and operating Page content
safely using direct HTTPS calls.
Best fit
- - Page posting automation with images (DALL-E generated or external URL)
- Token management (short-lived → long-lived → page token)
- Retry-safe, rate-limit-aware production pipelines
Not a fit
- - Personal profile posting (not supported by Graph API for third-party apps)
- Ads / Marketing API workflows
- Browser-based OAuth flows
Quick orientation
CODEBLOCK0
Token Flow
Short-lived User Token (1-2h)
↓ GET /oauth/access_token?grant_type=fb_exchange_token
Long-lived User Token (60 days)
↓ GET /me/accounts
Page Access Token (never expires*)
*Until user changes password or revokes app.
Required Environment Variables
CODEBLOCK2
Key API Endpoints
Post text
CODEBLOCK3
Upload photo (unpublished)
CODEBLOCK4
Post with photo
CODEBLOCK5
Scheduled post
CODEBLOCK6
Required Permissions
| Permission | Purpose |
|---|
| INLINECODE0 | Create/edit posts |
| INLINECODE1 |
Read reactions, comments |
|
pages_show_list | List managed pages |
|
public_profile | Basic user identity |
Rate Limits
- - 200 calls/hour/user token
- Implement retry with exponential backoff (see fbpublisheragent.py)
- POST 4-5 times/day max per Page for safety
Security
- - Never log tokens or app secrets
- Store all secrets in .env (ignored by git)
- Validate webhook signatures if using webhooks
- Monitor token validity daily with a cron job
Facebook Graph API 技能(高级)
目的
面向生产的指南,用于构建 Facebook Graph API 工作流以管理页面:
发布帖子(文本+图片)、管理令牌,并通过直接 HTTPS 调用安全操作页面内容。
最佳适用场景
- - 带图片的页面发布自动化(DALL-E 生成或外部 URL)
- 令牌管理(短期令牌 → 长期令牌 → 页面令牌)
- 支持重试、感知速率限制的生产级流水线
不适用场景
- - 个人主页发布(Graph API 不支持第三方应用)
- 广告/营销 API 工作流
- 基于浏览器的 OAuth 流程
快速导航
agents/fbtokenhelper.py ← 获取并交换令牌(先运行此文件!)
agents/fbpublisheragent.py ← 向页面发布文本/图片
config.py ← 所有环境变量
testfbconnection.py ← 验证令牌是否有效
令牌流程
短期用户令牌(1-2小时)
↓ GET /oauth/accesstoken?granttype=fbexchangetoken
长期用户令牌(60天)
↓ GET /me/accounts
页面访问令牌(永不过期*)
*除非用户更改密码或撤销应用授权。
必需的环境变量
env
FB
APPID=... # 来自 Meta for Developers
FB
APPSECRET=... # 应用密钥
FB
PAGEID=... # 目标粉丝页面 ID
FB
PAGEACCESS
TOKEN=... # 来自 fbtoken_helper.py
关键 API 端点
发布文本
POST /v21.0/{page_id}/feed
message=...
accesstoken={pagetoken}
上传图片(未发布)
POST /v21.0/{page_id}/photos
url={image_url}
published=false
accesstoken={pagetoken}
→ 返回:{ id: PHOTO_ID }
发布带图片的帖子
POST /v21.0/{page_id}/feed
message=...
attachedmedia[0]={mediafbid:PHOTO_ID}
accesstoken={pagetoken}
定时发布
POST /v21.0/{page_id}/feed
message=...
scheduledpublishtime={unix_timestamp}
published=false
accesstoken={pagetoken}
必需权限
| 权限 | 用途 |
|---|
| pagesmanageposts | 创建/编辑帖子 |
| pagesreadengagement |
读取反应、评论 |
| pages
showlist | 列出管理的页面 |
| public_profile | 基本用户身份 |
速率限制
- - 每个用户令牌每小时 200 次调用
- 实现指数退避重试(参见 fbpublisheragent.py)
- 安全起见,每个页面每天最多 POST 4-5 次
安全
- - 切勿记录令牌或应用密钥
- 将所有密钥存储在 .env 文件中(被 git 忽略)
- 如果使用 webhook,验证 webhook 签名
- 通过 cron 作业每日监控令牌有效性