forked from sns-sdks/python-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish_facebook_post.py
More file actions
32 lines (24 loc) · 993 Bytes
/
Copy pathpublish_facebook_post.py
File metadata and controls
32 lines (24 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
This is an example for facebook page publish a post.
Refer: https://developers.facebook.com/docs/graph-api/reference/page/feed#publish
"""
import os
from pyfacebook import GraphAPI
APP_ID = os.environ.get("APP_ID") # Your App ID
APP_SECRET = os.environ.get("APP_SECRET") # Your App secret
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN") # Your Access Token with the target page
def publish_simple_posts(page_id):
api = GraphAPI(app_id=APP_ID, app_secret=APP_SECRET, access_token=ACCESS_TOKEN)
data = api.post_object(
object_id=page_id,
connection="feed",
params={
"fields": "id,message,created_time,from",
},
data={"message": "This is a test message by api"},
)
print(data)
# {'id': 'xxx', 'message': 'This is a test message by api', 'created_time': '2022-06-01T03:49:36+0000', 'from': {'name': 'xx', 'id': 'xxxx'}}
return True
if __name__ == "__main__":
publish_simple_posts(page_id="meta")