LinkedIn organizationPageStatistics API returns 400 PARAM_INVALID for timeIntervals (REST.li 2.0)

I am calling the LinkedIn organizationPageStatistics endpoint using the REST.li 2.0 API, but I keep getting a 400 PARAM_INVALID error related to the timeIntervals parameter.

According to the official documentation (li-lms-2025-11), timeIntervals is an object, not a list.

Error response

{
  "errorDetailType": "com.linkedin.common.error.BadRequest",
  "message": "Invalid param. Please see errorDetails for more information.",
  "errorDetails": {
    "inputErrors": [
      {
        "description": "Invalid value for param; wrong type or other syntax error",
        "input": {
          "inputPath": {
            "fieldPath": "timeIntervals"
          }
        },
        "code": "PARAM_INVALID"
      }
    ]
  },
  "status": 400
}

Code

def fetch_linkedin_analytics_and_save(user, account):
    organization_urn = f"urn:li:organization:{account.page_id}"
    access_token = account.access_token
    start_ms = int(
                (datetime.now() - timedelta(days=90)).timestamp() * 1000)
    end_ms = int(datetime.now().timestamp() * 1000)

    base_url = "https://api.linkedin.com/rest/organizationPageStatistics"

    LINKEDIN_API_VERSION = os.environ.get("LINKEDIN_API_VERSION", "202511")

    headers = {
            "Authorization": f"Bearer {access_token}",
            "Linkedin-Version": LINKEDIN_API_VERSION,
            "X-Restli-Protocol-Version": "2.0.0",
            "Content-Type": "application/json"
    }

    time_intervals_str = f'(timeGranularityType:DAY,timeRange:(start:{start_ms},end:{end_ms}))',
    params = {
            "q": "organization",
            "organization": organization_urn,
            'timeIntervals.timeGranularityType': 'DAY',
            'timeIntervals.timeRange.start': start_ms,
            'timeIntervals.timeRange.end': end_ms
    }
    print(params)

    response = requests.get(base_url, headers=headers, params=params)

    if response.status_code != 200:
        print(response.text)

Attempt 1 – REST.li object string

time_intervals_str = (
    f"(timeGranularityType:DAY,"
    f"timeRange:(start:{start_ms},end:{end_ms}))"
)

params = {
    "q": "organization",
    "organization": organization_urn,
    "timeIntervals": time_intervals_str
}
print(time_intervals_str)
#(timeGranularityType:DAY,timeRange(start:1758957224365,end:1766733224365))

Attempt 2 – Flattened parameters

params = {
    "q": "organization",
    "organization": organization_urn,
    "timeIntervals.timeGranularityType": "DAY",
    "timeIntervals.timeRange.start": start_ms,
    "timeIntervals.timeRange.end": end_ms
}

Your help will be appreciated

Reference https://learn.microsoft.com/en-us/linkedin/marketing/community-management/organizations/page-statistics?view=li-lms-2025-11&tabs=curl

Вернуться на верх