Accessing Google Calendar API with Unity (Android)

My dream: Showing my own Google Calendar events in my own privat app (no public store), no multiple users.

I know, I know, you can find other threads with this topic, but I can’t find a working solution and I’m confused when it comes to authorizing with Google. The good: I managed it to get my calendar events on my Windows Unity editor following Googles advices to integrate Calendar API, the bad: I don’t know how to this on Android?

  1. Do I need Firebase Google Auth to sign in my own calendar on an Android device, or is Google Calendar API something else, working with own Oauth credentials?
  2. Has anybody a working example how it would work on Android?

I’m familiar with Google Cloud Console, Firebase Console, Oauth, enabling Signing-In methods, getting google json files for Android, iOS, Webclient and so an but I mix it all up and don’t know if there is an easy solution to get my calendar events.

Please help, I lost days with this. Thanks guys.

This is going to be almost all Android stuff, not Unity stuff.

You need to find out what sequence of API calls in Android do what you want.

Unity has an Android interface layer that may contain enough flexibility to do what you want.

https://docs.unity3d.com/ScriptReference/AndroidJavaClass.html

Thanks for your reply, but this doesn’t help. I know how to call external Java methods with Unity but this is not necessary with Google C# examples:

https://firebase.google.com/docs/auth/unity/google-signin

and there are .NET libraries packed as DLLs to use them in Unity:

https://github.com/SaadAnees/Unity-Google-Calendar

But they are made for accessing the calendar on a desktop plattform.

The problem is to sign in as a user before you can access the calendar api. And so I startet to read about Google Sign In Firebase API calls. I’m able to login with Firebase but I don’t know how I use my credential to access my Google Calendar on Android. I feel that Google Calendar belongs to Google Cloud services but there is no need to login with Google Firebase Auth… just a guess.

If there is somebody who used Google Calendar in Unity on an Android device… I need your knowledge.

any solution ?

I built this app in Unity with an Android calendar integration:

It took a lot of wrestling. Ultimately I had to build an aar that included the UnityActivity extension as outlined in this tutorial:

The Java code looks like this:

public class CalendarsActivity
        extends Activity
        implements CalendarsProxy.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("OverrideActivity", "onCreate called!");
        super.onCreate(savedInstanceState);

        this.mCalendars = new ArrayList<>();

        this.mCalendarsAdapter = new CalendarsArrayAdapter(this, 0, this.mCalendars);

        this.mCalendarsQueryHandler = new CalendarsProxy(this.getContentResolver());
        this.mCalendarsQueryHandler.registerListener(this);
        CalendarsActivity.ctx = this;
        this.queryCalendars();
    }

    // called from Unity
    public String getAllCalendarNames() {
        String ret = "";
        for (int i = 0; i < this.mCalendars.size(); i++) {
            ret += this.mCalendars.get(i).getDisplayName() + "|";
        }

        return ret;
    }

There is more code to make this all work that I’m not including here, because it references other source code from a Calendar Library that I can’t find. If I locate it, I’ll provide a link and also a fork of the code (I was working on this 3 years ago).

The aar that is assembled from this Java code is placed in the Android/Plugins folder. It is then called from C# code like this:

Dictionary<string, MyEvent[]> calendarsAndEvents = new Dictionary<string, MyEvent[]>();
            using AndroidJavaClass javaClass = new("com.unity3d.player.UnityPlayer");
            using AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject calObj = activity.Call<AndroidJavaObject>("getAllCalendarNames");

            if (calObj.GetRawObject().ToInt32() == 0)
            {
                // this returns empty if calendars are empty
                calObj.Dispose();
                return calendarsAndEvents;
            }
                   
            byte[] calendarNamesBytes = calObj.GetRawObject().ToInt32() != 0
                ? AndroidJNIHelper.ConvertFromJNIArray<byte[]>(calObj.GetRawObject())
                : Array.Empty<byte>();
            calObj.Dispose();

The Android Manifests (one for the app and one for the calendar plugin) need to include the appropriate permissions and combine together and override the main activity. There are instructions in the Unity tutorial for this.

Recently I discovered that target SDK 30 and IL2CPP compilation is required to publish your app on the Google Play store.

Hope this helps as a starting point. I wish I knew all of the above when I started on this 3 years ago :slight_smile:

Good But Not Enough.

I was randomly looking on an old hard drive, and I found the repository that I mentioned above.

The EventsActivity.java exposes Google calendar events. Calling into this as an arr allows Unity to retrieve these events: