Using Google calender api with flutter
Introduction⌗
In this blog we are going to use Google Calender API with flutter to create an event in google calender. We are going to build a simple flutter app in which there will be a date picker and an input field for adding name for the event. The app will automatically add the event in google calender using the Calender API.
Steps :-⌗
-
Go to the Google API Dashboard (https://console.cloud.google.com/apis/dashboard)
-
Sign in with your gmail.
-
You will see an interface like this. Click on My Project
All your projects should be listed here
-
Create a new project
-
Add your project name and create a new project
-
Return to the desktop and open your newly created project from the list.
-
Click on Enable Api and Services from the menu
-
Click on ENABLE API AND SERVICES
-
Search for the Google Calender API and enable it. Now return to the Dashboard.
-
Now go the the OAuth Consent Secrren and choose External. Choosing external will allow any user that you will add further in the test users section to test the app
-
Now add necessary information and click on continue
-
Now click on Add or Remove scopes and add Google Calender API in the scope and click on update
-
In the next section add test users and click on save and continue. Review your OAuth Consent summary and continue
-
Create a new flutter project using the command
-
Now we have to create OAuth Client ID. Go to the credentials section click on Create Credentials.
-
Click on OAuth client ID
-
Select Application type to android and fill the necessary input fields. You can get the package name from AndroidManifest.xml file which is located in the /android/app/src/main/ folder of the project folder. To get SHA1 key enter the following command in your terminal.
Now after adding the SHA1 key click on create. You will get your new OAuth client ID.
Now time to code
-
Create a simple screen with two Date and Time picker (One for event start time and one for event end time) and an input field for entering the event name.
main.dart
Home Screen will look like this
-
Add to calender function
calender_client.dart
Add required Packages⌗
googleapis -> for using google calender api
googleapis_auth -> for creating OAuth access Token
extension_google_sign_in_as_googleapis_auth -> for creating AuthClient instances for google calender API
-
Import the packages
-
Now SignIn into google under the scope of google calender api
Here we are using calenderScope (https://www.googleapis.com/auth/calendar) to add event into google calender
-
After signin create an AuthClient instance which will hold the OAuth access tokens and pass it to the calender api
-
Now create an event to add into the calender
-
Now finally insert the event into calender
The whole calender_client.dart will look like this⌗
-
Now finally wire the addToCalender function into the Add button in home page
Final Result⌗
Done⌗
You can find the github repo here