Flutter: Use Google Calendar API — Adding the events to Calendar
Introduction:
Google Calendar API can help to Get, Delete, Create events and many other things.
I am going to write steps to show how to create events in Google Calendar through Flutter app.
Beginning
You may want to skip steps from 1–11 if you’ve already Created the google console Project and Enabled Calendar API.
1 — Go to Google APIs dashboard https://console.developers.google.com/.
2 — Login through your Gmail account. (If you are already logged in then you may not need to log in again)
You will be now at dashboard like the following picture
3 — Click the My Project (don’t worry if you see some other name).
You will see the list of all Projects.
4 — Create a new Project
5 — You can create a new Project here.
Note: You may see some warning like this You have 2 projects remaining in your quota. Request an increase or delete projects. This is the limit of the project that google clouds provide a user to create. Of course, you can increase it by requesting them.
You may see error You’ve reached your project limit. You can create more projects after you request a project limit increase. That means you have used your all limits. Now either requests them or delete at least one previous project to create a new project.
6 — After Creating, you will be returned to dashboard. Confirm that the name of project is similar to that you have created. If not open the list of all projects by clicking here and Selecting your desired Project
7 — Click ENABLE APIS AND SERVICES
8 — Search for Google Calendar API. And open it.
9 — Enable This API
10 — Return to your Dashboard.
11 — Click on Credentials
12 — Click on Create Credentials. Then on OAuth Client ID.
13 — Select application type. Since we are using flutter we will need to create two OAuth Client IDs one for Android and one for iOS. And then give the details asked like Bundle ID (for iOS) and Package Name and SHA-1 certificate fingerprint for Android.
Now you will have two OAuth client IDs one for android and one for iOS.
Your client ids will be like
***********************c4q2r.apps.googleusercontent.com
Some Coding by your ownself
Design the simple app in which we will take Event start Time, End Time and Event Name as Input(that’s not the part of this article I am just showing my final simple design) Following is the basic design.
Note: I am using flutter_datetime_picker package to take Date Time Input from user. You can use any other method to take Date Time Input from user. But make sure start and End date is DateTime object.
Insert Event Button does nothing right now. But soon it will start working.
Now We will start real work to add an event to calendar
Add Packages
Add Following Packages
googleapis (I am using 0.55.0 version) — To use Google Calendar API
googleapis_auth (I am using 0.2.12 version) — To create OAuth access Credentials.
url_launcher (I am using 5.5.0 version) — To launch URL in browser to ask user for permission to access.
Import Packages
Import following packages
Create Scopes
Create OAuth scope to access Calendar.
We will be using CalendarScope (https://www.googleapis.com/auth/calendar) to create event in Calendar.
There are other scopes too. You can read about them at Google Calendar API Docs
Creating ClientID Object
Create ClientID using following code
Event Object
Create an Event object.
We will be creating the object of Event.
Note: summaryText is Event name input taken from user. startTime and endTime are DateTime object taken input by user using flutter_datetime_picker package
Finally Inserting the Event
Following is the code to to take User’s consent and inserting the event to Google Calendar.
Note: We are using primary Calendar of User that’s why in code its calenderId=primary.
But if you want to use any other User’s Calendar you can call calendar.calendarList.list() to get list of Calendar and use its ID.
Final Output
Conclusion
In this article we learnt about creating an event in user’s google Calendar through our app using Google Calendar API. In the next articles we will be retrieving the events from Google Calendar.
Note: to release the app to general public you will need to setup the consent screen and application need to be verified by Google. You can read more about this at https://support.google.com/cloud/answer/9110914
You can find the project Repository here.
Thanks for reading my article so far, please give me your feedback. And if you liked my article make sure to give a clap
Fun Fact: You can clap up to 50 times if you really found my article helpful.