- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
We usually rely on Google and Microsoft for our mailbox and calendars, forgetting (if we are iPhone users) that an iCloud account is at the core of our phones.
Let’s learn how to connect our iCloud account to Nylas to access our email, events and contacts.
In this tutorial, we’ll use the Nylas Python SDK to access the Nylas APIs, since Python is one of the most popular programming languages right now.
If you already have the Nylas Python SDK installed and your Python environment configured, skip to the next section. Also, you can check the Nylas Quickstart Guides.
If this is your first time working with the Nylas SDK, I recommend reading the post How to Send Emails with the Nylas Python SDK, where I explain the basic setup.
We’re going to assume that this is our very first time interacting with an iCloud account, so naturally, we don’t know where to begin. The one thing we do know is that we have an AppleId email account which we use to connect to iCloud.
We’ll go to our Nylas Dashboard and log in. Click on our application and then on Connect an email account:
Once in there, we’re going to try to connect an account. Leave the scopes checkboxes selected, and enter our Apple ID email address:
The selected provider on the login prompt changes depending on which email address we use as our Apple ID:
Enter your iCloud account password and see what happens. You should see it fail with an error message like this:
So, most likely, we’re going to change the provider to iCloud account and make sure we have two-factor authentication enabled and will create an app password (An app password is a token of authorization that we can use in several services).
We can do this using any of the following three methods:
This will add an extra layer of security to your account, so every time you log in you will need to approve it:
We can now continue with the next step, which is creating a new app password.
If you are no longer connected to appleid.apple.com, log in again.
In the Sign-in and Security section, select App-Specific Passwords:
A pop-up window appears and prompts you to generate a new app-specific password
Click the plus sign, enter “Nylas” as a name, and copy the generated password.
Since we know that we’re logging into the iCloud account, we can go back to the Nylas dashboard, click Select a different provider and select iCloud from the list. Then use our new App password, and try to log in again, just to fail one more time:
Why? Well, it turns out that our Apple ID email is not exactly the same as our iCloud email. Do we even have an iCloud email?
While you’re supposed to be able to use your iPhone, iPad or Mac to create an iCloud email account, doing it on the Mac doesn’t always work, so we’ll do it on the iPhone.
Go to Settings → your name → iCloud → Tap on iCloud Mail then follow the instructions.
If it fails, simply log out and log in again and then try again.
Our iCloud Mail account should be ready to use.
Finally, we should be able to log in using our new iCloud email account and App Password.
This time, it should work and will look like this on our Nylas dashboard.
When you do this, Nylas creates an access token. Make sure you copy it and store it in a safe place.
Give it a couple of minutes to sync and get everything into place (Nylas and Apple).
Now that everything is working as expected, let’s test it out by creating a simple Python script to read out our mailbox. This should be almost as empty since we just created it.
On your .env file add the following at the bottom of the file:
ICLOUD = “YOUR_ICLOUD_ACCESS_TOKEN”
Remember that Nylas access token you saved? That’s what you put here.
Next, we move on to the Python script. Let’s call it Read_iCloud_Inbox.py and copy and paste the following code:
# Import packages import os from nylas import APIClient from dotenv import load_dotenv load_dotenv() # Initialize Nylas client nylas = APIClient( os.environ.get("CLIENT_ID"), os.environ.get("CLIENT_SECRET"), os.environ.get("ICLOUD"), ) # Call the messages endpoint messages = nylas.messages # Display the Subject, who sends the email for message in messages: print("Subject: {} | From: {}".format(message.subject, message.from_)) print("\n")
We can run this code by typing the following in a terminal window:
$ python3 Read_iCloud_Inbox.py
Nice, we have one welcome email from iCloud!
Now, let’s take a look at iCalendar, since we most likely want to access our events as well.
For this, we will need two scripts: one to get the ids of our calendars and the second to access the events contained in each calendar.
Let’s start with getting our calendars. Create a script called Read_iCloud_Calendars.py:
# Import packages import os from nylas import APIClient from dotenv import load_dotenv load_dotenv() # Initialize Nylas client nylas = APIClient( os.environ.get("CLIENT_ID"), os.environ.get("CLIENT_SECRET"), os.environ.get("ICLOUD"), ) # Call calendars endpoint calendars = nylas.calendars.all() # Display name and description for calendar in calendars: print("Id: {} | Name: {} | Description: {}".format( calendar.id, calendar.name, calendar.description))
This will bring back a bunch of calendars, but in this case, we’re only interested in the one named Blag. You will have different calendars for sure:
Let’s grab the Id as we’re going to use it on our next script which will be called Read_iCloud_Events.py:
# Import packages import os import datetime from datetime import date from nylas import APIClient from dotenv import load_dotenv load_dotenv() # Initialize Nylas client nylas = APIClient( os.environ.get("CLIENT_ID"), os.environ.get("CLIENT_SECRET"), os.environ.get("ICLOUD"), ) # Get today’s date today = date.today() # Specify from and to dates, so we take the whole day AFTER = int(datetime.datetime(today.year, today.month, today.day, 0, 0, 0).strftime('%s')) BEFORE = int(datetime.datetime(today.year, today.month,today.day, 23, 59, 59).strftime('%s')) # Call the events endpoint events = nylas.events.where(calendar_id="CALENDAR_ID", starts_after=AFTER, ends_before=BEFORE) # Print the Id, title, start, end and participants # Determine whether is a timed event or a whole day event for event in events: if "start_time" in event.when: print("Id: {} | Title: {} | Start: {} | End: {} | Participants: {}\n".format( event.id, event.title, datetime.datetime.fromtimestamp(event.when['start_time']).strftime('%Y-%m-%d %H:%M:%S'), datetime.datetime.fromtimestamp(event.when['end_time']).strftime('%Y-%m-%d %H:%M:%S'), event.participants )) else: print("Id: {} | Title: {} | Date: {} | Participants: {}\n".format( event.id, event.title, event.when["date"],event.participants ))
This will return all the events on that particular calendar:
With that, we have successfully integrated our iCloud account with Nylas.
To learn more about our Email APIs, go to our documentation Email API Overview.
To learn more about our Calendar APIs, go to our documentation Calendar API Overview.
You can sign up for Nylas for free and start building!
Don’t miss the action, join our LiveStream Coding with Nylas!
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.