Use this skill for Ronnie's Facebook Page operations, especially:
Required environment keys:
FACEBOOK_PAGE_IDFACEBOOK_PAGE_ACCESS_TOKENFACEBOOK_GRAPH_VERSION (optional, default v22.0)Meaning of each config item:
FACEBOOK_PAGE_ID: the numeric Facebook Page ID used in Graph API endpointsFACEBOOK_PAGE_ACCESS_TOKEN: the Page access token used to publish posts and test engagement actionsFACEBOOK_GRAPH_VERSION: optional Graph API version pin, e.g. v22.0Quick check:
bash "<base_dir>/scripts/check_env.sh"
FACEBOOK_PAGE_IDChoose one of these methods:
Run:
```bash
python3 - <<'PY'
import os, json, urllib.request, ssl
token = os.environ['FACEBOOK_PAGE_ACCESS_TOKEN']
url = f'https://graph.facebook.com/v22.0/me?fields=id,name&access_token={token}'
ctx = ssl.create_default_context()
with urllib.request.urlopen(url, context=ctx, timeout=30) as r:
print(r.read().decode())
PY
```
Expected result example:
```json
{"id":"123456789012345","name":"Your Page Name"}
```
The id value is your FACEBOOK_PAGE_ID.
FACEBOOK_PAGE_ACCESS_TOKENRecommended path:
pages_show_listpages_read_engagementpages_manage_postspages_manage_engagementpages_read_user_content (recommended for comment-related workflows)Why this matters:
FACEBOOK_PAGE_ACCESS_TOKENUse Meta Graph API Explorer or your app login flow to obtain a user token for the Facebook account that manages the target Page.
Call:
python3 - <<'PY'
import os, urllib.request, ssl, urllib.parse
app_id = os.environ['FACEBOOK_APP_ID']
app_secret = os.environ['FACEBOOK_APP_SECRET']
short_token = os.environ['FACEBOOK_USER_ACCESS_TOKEN']
params = urllib.parse.urlencode({
'grant_type': 'fb_exchange_token',
'client_id': app_id,
'client_secret': app_secret,
'fb_exchange_token': short_token,
})
url = f'https://graph.facebook.com/v22.0/oauth/access_token?{params}'
ctx = ssl.create_default_context()
with urllib.request.urlopen(url, context=ctx, timeout=30) as r:
print(r.read().decode())
PY
Expected result example:
{"access_token":"LONG_LIVED_USER_TOKEN","token_type":"bearer","expires_in":5183944}
Important:
expires_in around 5 million seconds is roughly 60 daysFACEBOOK_APP_IDFACEBOOK_APP_SECRETFACEBOOK_USER_ACCESS_TOKEN (short-lived user token)Run:
python3 - <<'PY'
import os, json, urllib.request, ssl
user_token = os.environ['FACEBOOK_USER_ACCESS_TOKEN']
url = f'https://graph.facebook.com/v22.0/me/accounts?access_token={user_token}'
ctx = ssl.create_default_context()
with urllib.request.urlopen(url, context=ctx, timeout=30) as r:
print(r.read().decode())
PY
Important:
FACEBOOK_USER_ACCESS_TOKEN should contain the long-lived user token, not the short-lived oneIn the returned JSON, find the target Page entry:
id as FACEBOOK_PAGE_IDaccess_token as FACEBOOK_PAGE_ACCESS_TOKENTest whether the token can directly read the target Page:
python3 - <<'PY'
import os, urllib.request, ssl
page_id = os.environ['FACEBOOK_PAGE_ID']
token = os.environ['FACEBOOK_PAGE_ACCESS_TOKEN']
url = f'https://graph.facebook.com/v22.0/{page_id}?fields=id,name&access_token={token}'
ctx = ssl.create_default_context()
with urllib.request.urlopen(url, context=ctx, timeout=30) as r:
print(r.read().decode())
PY
Expected result example:
{"id":"123456789012345","name":"Your Page Name"}
Important reminders:
Prefer Graph API first.
Use Python request execution if curl to graph.facebook.com is unstable in the current environment.
Try Graph API first only when the user explicitly wants permission verification or the token is believed to include comment/reply scopes.
If API returns permission errors such as (#200) You do not have sufficient permissions to perform this action, explain clearly that:
Use browser automation when:
python3 - <<'PY'
import os, urllib.request, urllib.parse, ssl
page_id = os.environ['FACEBOOK_PAGE_ID']
token = os.environ['FACEBOOK_PAGE_ACCESS_TOKEN']
version = os.environ.get('FACEBOOK_GRAPH_VERSION', 'v22.0')
message = 'Your post text here'
url = f'https://graph.facebook.com/{version}/{page_id}/feed'
data = urllib.parse.urlencode({
'message': message,
'access_token': token,
}).encode('utf-8')
req = urllib.request.Request(url, data=data, method='POST')
ctx = ssl.create_default_context()
with urllib.request.urlopen(req, context=ctx, timeout=30) as r:
print(r.read().decode())
PY
Expected success result:
{"id":"PAGEID_POSTID"}
python3 - <<'PY'
import os, urllib.request, urllib.parse, ssl
version = os.environ.get('FACEBOOK_GRAPH_VERSION', 'v22.0')
token = os.environ['FACEBOOK_PAGE_ACCESS_TOKEN']
post_id = 'TARGET_POST_ID'
message = 'Test comment from API'
url = f'https://graph.facebook.com/{version}/{post_id}/comments'
data = urllib.parse.urlencode({
'message': message,
'access_token': token,
}).encode('utf-8')
req = urllib.request.Request(url, data=data, method='POST')
ctx = ssl.create_default_context()
try:
with urllib.request.urlopen(req, context=ctx, timeout=30) as r:
print(r.read().decode())
except urllib.error.HTTPError as e:
print(e.read().decode())
PY
Typical failure observed in Ronnie's setup:
{"error":{"message":"(#200) You do not have sufficient permissions to perform this action"}}
If the user's goal includes posting, reading comments, leaving comments, and replying to comments, recommend this permission set:
pages_show_listpages_read_engagementpages_manage_postspages_manage_engagementpages_read_user_content (recommended)Also remind the user:
Conclusion:
curl fails but Python urllib worksConclusion:
If API still fails, switch to browser automation and ask user to log in if needed.
Be concrete and short:
共 1 个版本