Creating Calendar events for a user

Jump to solution
maguire
Community Champion

I am trying to create a calendar event for a user (other than myself) - so that as part of a course I can provide reminders (in this case a 10th month reminder that the student's thesis should be finished before the 12th month from the time the student started their thesis project). The function I am using is:

def create_calendar_event(user_id, cal_date, title, description):     # Use the Canvas API to get the calendar event     # POST /api/v1/calendar_events     url = "{0}/calendar_events".format(baseUrl)     if Verbose_Flag:         print("url: " + url)      context_code="user_{}".format(user_id)     #context_code="course_{}".format(user_id)     print("context_code={}".format(context_code))     date_time_start=datetime.datetime.strptime(cal_date, "%Y-%m-%d")     date_time_end=date_time_start     print("date_time_start is {}".format(date_time_start))      payload={'calendar_event[context_code]': context_code,              'calendar_event[title]': title,              'calendar_event[description]': description,              'calendar_event[start_at]': date_time_start,              'calendar_event[end_at]':   date_time_end     }     r = requests.post(url, headers = header, data=payload)     if Verbose_Flag:         print("result of creating a calendar event: {}".format(r.text))      if r.status_code == requests.codes.ok:         page_response=r.json()         return page_response     else:         print("status code={}".format(r.status_code))     return None

What I have observed is that if I use the course context (i.e., course_5) I can create the event in the course calendar. If I use a user context, then if I use my own user if, it works; but if I use another user's id, then I get a 401 error. I am doing this as the site admin user. My suspect that the problem comes from the "if authorized_action(@event, @current_user, :create)" saying that the user is not allowed to create user context calendar events for another user. Is this intentional or is there some permission setting I am missing?

Note, that an alternative could be to add an assignment for the user that has a due date 10 months into the future; however, this would mean managing the assignments at the individual student level (as students start on a variety of dates).

1 Solution
pklove
Community Champion

When trying to it for another user, have tried masquerading?  That is, add ?as_user_id=user to the endpoint.

It works in some other situations where you have to be the user.

View solution in original post