Forums

Is scheduling tasks with API free on pythonanywhere

I'm trying to create a scheduled task through API and I get this response:

Failed to create cron job: 403 {"detail":"You do not have permission to perform this action."}

NOTE: I HAVE A FREE ACCOUNT

Code responsible for that result:

async def cron_command(user_id, time):
enter code here
api_url = "https://www.pythonanywhere.com/api/v0/user/*****/schedule/"

api_key = "*********"

command = f"python3 /home/*******/*****/******/tasks.py {user_id}"

time_obj = datetime.strptime(time, "%H:%M")
hour = time_obj.hour
minute = time_obj.minute

data = {
    "enabled": True,
    "command": command,
    "hour": hour,
    "minute": minute,
    "interval": "daily"}

headers = {
    "Authorization": f"Token {api_key}",
    "Content-Type": "application/json"
}

response = requests.post(api_url, headers=headers, data=json.dumps(data))

if response.status_code == 201:
    print("Cron job created successfully.")
else:
    print(f"Failed to create cron job: {response.status_code}")
    print(response.text)

what is time there?

It holds a time value with this structure: 10:09. Then I extract the hour and the minutes

And how do you run that function?

@fjl Thanks for your time and effort first, I call that function inside another one as follows:

async def edit_cron_time(update, context):
user_id = update.message.from_user.id
new_cron_time = update.message.text
cron_type = context.user_data.get('cron_settings')
res = cron_seed(user_id, cron_type, new_cron_time)
if res == True:
#HERE:
    await cron_command(user_id,new_cron_time)

    await update.message.reply_text(f"تم التحديث إلى:  {new_cron_time}")
else:
    await update.message.reply_text("لا يمكن تحديث التوقيت في الوقت الراهن")

return ConversationHandler.END

Note that they're all in the same file.

The only validation that I can think of that would return the specific error that you're getting would be if the API key you're providing in your headers is not one for the user in the URL -- is there any chance that that is the case? Perhaps it would be worth printing out the value of api_key just before the post to double-check.

Unfortunately, the "You do not have permission to perform this action." is all I get from pythonanywhere API. I have re-checked the API token and links. I created the job manually to see if I had a problem with my code but it worked fine. I thought maybe free accounts can't create jobs through API?

They definitely can create jobs through the API, so I'm a bit perplexed too. Do you see the right API key in the output of the script if you add something to print it?

I do see the correct api.

Sharing an example of creating a task through api would be great if it is available

Your code above is fine -- that's why I'm perplexed. Just to verify, I created a free account and ran this:

import requests
import json

api_url = "https://www.pythonanywhere.com/api/v0/user/USERNAME/schedule/"

api_key = "XXXXXXXXXXXXXXXXX"

command = f"sleep 3600"

hour = 12
minute = 15

data = {
    "enabled": True,
    "command": command,
    "hour": hour,
    "minute": minute,
    "interval": "daily"}

headers = {
    "Authorization": f"Token {api_key}",
    "Content-Type": "application/json"
}

response = requests.post(api_url, headers=headers, data=json.dumps(data))

if response.status_code == 201:
    print("Cron job created successfully.")
else:
    print(f"Failed to create cron job: {response.status_code}")
    print(response.text)

As you can see, the code is just a slightly stripped-down version of yours, with a fixed time and command. When I ran it I got "Cron job created successfully.", and on the "Tasks" page, I could see a task to run sleep 3600 was scheduled at 12:15 daily.

Hmm, bit of a stab in the dark here, but I notice that your username has a mixture of capital letters and lower-case. Are you matching the same exact casing in the API request's URL, with capital E and capital O?