Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iCloud Calendar Requests

I'm trying to access the my iCloud calendars through curl (using this as a test before I write any code), but am getting unauthorised access errors. Has anyone had any luck in getting this to work? I've used this to extract my user ID and have determined by server ID by emailing myself an event invite.

Command run

curl -u <apple email ID> https://<server id>-caldav.icloud.com/<user id>/calendars

Response

<html><head><title>Unauthorized</title></head><body><h1>Unauthorized</h1><p>You are not authorized to access this resource.</p></body></html>

Edit

I've tried to replicate this query using fiddler but keep getting a 502 response - connection failed with SecurityException.

like image 794
clicky Avatar asked Sep 15 '25 06:09

clicky


1 Answers

To access iCloud calendars or reminders you use an IETF protocol called CalDAV. Building a CalDAV client is a great introduction on how to do this. With the above you are just throwing a GET request at the API ...

Using curl it is a 3-step process to get to, say, the calendar names. First you need to figure out the URL representing your iCloud account (called a Principal in WebDAV speak):

curl -s -X PROPFIND -u "$APPLEID" -H "Depth: 0" \
  --data "<propfind xmlns='DAV:'><prop><current-user-principal/></prop></propfind>" \
  https://caldav.icloud.com/

If you can't get to authenticate against that, you are using wrong credentials, you may have enabled two-step verification for your iCloud account, or it is past 2017-06-15: "App specific passwords will be required to sign in to iCloud with third party apps from June 15". In this case you need to setup an app-specific password for CalDAV access.

The curl above shows you the URL of your account record, it looks something like https://caldav.icloud.com/347723822/principal/. The next step is figuring out where your calendars are hosted. That is stored in the account's calendar-home-set property of the principal and can be accessed like so:

curl -s -X PROPFIND -u "$APPLEID" -H "Depth: 0" \
  --data "<propfind xmlns='DAV:' xmlns:cd='urn:ietf:params:xml:ns:caldav'><prop><cd:calendar-home-set/></prop></propfind>" \
  https://caldav.icloud.com/827267162/principal/

This gives you the reference to the cluster the actual calendars lives on, it looks something like https://p22-caldav.icloud.com:443/827161622/calendars. Lets say you want to get the names of all the calendars the user has:

curl -s -X PROPFIND -u "$APPLEID" -H "Depth: 1" \
  --data "<propfind xmlns='DAV:'><prop><displayname/></prop></propfind>" \
  https://p42-caldav.icloud.com:443/28377271/calendars/ \
| grep displayname

Hope this gets you started.

like image 91
hnh Avatar answered Sep 17 '25 20:09

hnh