我想使用 python 通过他们的 API 在 cron-job.org 上创建一个 cron 作业。
结果,我得到404。
我不知道到底是什么没找到
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}')
我尝试更改 command_url 以防万一,并搜索可能的 api 链接,但结果相同。
我发现我们应该使用 put 方法而不是 post
此外,API URL 必须是:
api_url = "https://api.cron-job.org/jobs"
最后,参数的最低限度应该是:
"job": {
"url": command_url,
"enabled": True,
"saveResponses": True,
"schedule": {
"timezone": "GMT",
"expiresAt": 0,
"hours": [hour],
"minutes": [minute],
"mdays": [-1],
"months": [-1],
"wdays": [-1]
}
}