[Python] Having a hard time dealing with cron-job.org API

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I want to create a cron job on cron-job.org through their API using python.

    As a result, I get 404.

    I don't know exactly what is not found

    from datetime import datetime
    import requests, json

    # Cron-job.org API URL
    api_url = "https://cron-job.org/api/1.0/user/cronjobs"
    # api_url = "https://api.cron-job.org"

    # Your cron-job.org API key
    api_key = "*******************************************"

    # Cron job details
    command_url = f"https://httpbin.org/json"

    # Extract hour and minute
    try:
    hour = 10
    minute = 30

    # Prepare cron job schedule (cron-job.org takes time in hour and minute, not the standard cron format)
    schedule = {
    "minute": minute,
    "hour": hour,
    "mdays": ["*"], # Every day of the month
    "months": ["*"], # Every month
    "wdays": ["*"], # Every day of the week
    "enabled": True,
    "url": command_url
    }

    # Create headers with API key for authentication
    headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"}

    # Make the API request to create the cron job
    response = requests.post(api_url, headers=headers, data=json.dumps(
    schedule))

    # Check if the cron job was created successfully
    if response.status_code == 201:
    print("Cron job created successfully.")
    else:
    print(f"Failed to create cron job: {response}")
    print(response.text)
    except Exception as e:
    print(f'Errrrrrror: {e}')


    I tried to change the command_url just in case and also search for possible api links but same resault.

    Continue reading...

Compartilhe esta Página