You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API或Python版Facebook SDK实现帖子反应与分享?

Hey there! Glad you're expanding your Facebook SDK functionality beyond just likes. Let's walk through how to add other reactions and implement post sharing with the Python SDK, building on the code you already have.

Adding Custom Reactions to Facebook Posts

The put_like() method you're using only handles the default "Like" reaction. To add other reactions (Love, Wow, Haha, Sad, Angry), you'll use the put_object() method targeting the reactions connection of the post.

Supported Reaction Types

Valid values for the type parameter are:

  • LIKE (matches what put_like() does)
  • LOVE
  • WOW
  • HAHA
  • SAD
  • ANGRY

Code Example

import facebook

# Initialize your GraphAPI instance as you already do
graph = facebook.GraphAPI(token)
post_id = "YOUR_TARGET_POST_ID"

# Add a "Love" reaction to the post
try:
    graph.put_object(
        parent_object=post_id,
        connection_name='reactions',
        type='LOVE'
    )
    print("Love reaction added successfully!")
except facebook.GraphAPIError as e:
    print(f"Error adding reaction: {e}")
Sharing Facebook Posts

Sharing a post can be done in two common ways: sharing an existing post directly to your timeline, or creating a new post that links to the original content. Let's cover both.

1. Share an Existing Post to Your Timeline

This replicates the "Share" button behavior on Facebook, creating a direct share of the original post on your feed.

# Share an existing post to your authenticated timeline
try:
    # "me" refers to the user the access token is linked to
    share_response = graph.put_object(
        parent_object='me',
        connection_name='feed',
        object_id=str(post_id),
        message="Check out this cool post!"  # Optional custom share message
    )
    print(f"Post shared successfully! Share ID: {share_response['id']}")
except facebook.GraphAPIError as e:
    print(f"Error sharing post: {e}")

2. Create a New Post Linking to the Original Content

If you want to make a standalone post that references the original (instead of a direct share), use this approach:

# Create a new post that links to the original Facebook post
try:
    post_response = graph.put_object(
        parent_object='me',
        connection_name='feed',
        link=f"https://www.facebook.com/{post_id}",
        message="Here's a post I wanted to share with you all!"
    )
    print(f"New post created successfully! Post ID: {post_response['id']}")
except facebook.GraphAPIError as e:
    print(f"Error creating post: {e}")

Critical Permission Notes

Make sure your access token includes these required permissions:

  • For adding reactions: user_posts (to access the target post) and publish_reactions (to add reactions)
  • For sharing posts: publish_posts (to post to your timeline) and user_posts (to access the original post)
  • If working with a Facebook Page instead of a personal account, use permissions like pages_manage_posts and pages_read_engagement

Always test with a test user first to avoid hitting rate limits or permission blocks!

内容的提问来源于stack exchange,提问作者John Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:59:15