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

使用Instagram Graph API的since/until参数筛选帖子异常问题求助

解决Instagram Graph API since/until 参数返回超出时间范围内容的问题

我之前也碰到过一模一样的坑!核心问题是你误解了API参数的要求:Instagram Graph API的since和until参数不接受YYYY-MM-DD格式的日期字符串,它要求传入的是Unix时间戳(以秒为单位)。

问题根源分析

你当前调用里用了since=2018-04-01和until=2018-04-30,API无法正确解析这种日期格式,直接忽略了这两个参数,自然就返回了所有可获取的媒体内容,出现超出时间范围的结果。另外你之前把参数直接拼在connection_name里的写法,也不符合get_connections方法的设计逻辑,容易导致参数解析异常。

修正后的代码实现

先把日期转换为Unix时间戳,Python里用datetime模块就能轻松处理:

import facebook
from datetime import datetime

# 初始化Graph API实例
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)

# 把目标日期转换为秒级Unix时间戳
start_date = datetime(2018, 4, 1)
# 加上时分秒确保覆盖4月最后一天的所有帖子
end_date = datetime(2018, 4, 30, 23, 59, 59)
since_timestamp = int(start_date.timestamp())
until_timestamp = int(end_date.timestamp())

# 正确调用API:参数以关键字形式传入,而非拼接在路径中
posts = graph.get_connections(
    profile['id'],
    'media',
    fields='caption,permalink,timestamp',
    since=since_timestamp,
    until=until_timestamp
)

获取帖子Insights的方法

要拿到每篇帖子的insights,你可以遍历返回的帖子列表,对每个帖子ID调用get_connections获取指定指标(比如曝光、触达、互动量等):

for post in posts['data']:
    post_id = post['id']
    # 获取指定指标的insights数据
    insights = graph.get_connections(
        post_id,
        'insights',
        metric='impressions,reach,engagement'
    )
    # 将insights数据和帖子信息合并
    post['insights'] = insights['data']
    print(post)

⚠️ 注意:获取insights需要你的应用拥有对应权限,且账号是Instagram企业/创作者账号,同时access_token需要包含instagram_basic、pages_show_list等必要权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:22:33