Danny
Danny

Reputation: 121

C# Desktop application using Google Calendar API .NET v3 -- Howto?

Probably not the only one asking this, but I found nothing that works...It's like a jungle ;)

I have a C# desktop app (5-10 user) that I want to connect to my Google calendar. I want to add, move, delete, update events in the calendar. I downloaded an installed the last .Net api v.3.

From what I read, the difficult part is the authenticate/connecting procedure. I suppose that we need to use OAuth 2.0 authentication ? I want my user to authenticate once, not each time they want to make a action (add, move, delete, update).In some sample I get we have to cut and paste a token from a web page into a Console...not really user friendly. And how to authenticate only 1 times (or just put the token directly in code to not authenticate ?) ?

Now, were I can have a good working sample code in C# to make that ?

Thanks you very much for helping !!

Upvotes: 2

Views: 5772

Answers (3)

Tino Hager
Tino Hager

Reputation: 1079

I have today the same issue and found now the solution

  1. Prepare Api Access https://console.developers.google.com/apis/dashboard

    1. Press button "Enable Apis and services"
    2. Search "Calendar" and choose "Google Calendar API"
    3. Press button "Create Project"
    4. Create Project
    5. Press "Enable"
    6. Press button "Create credentials"
    7. Choose "Where will you be calling the API from?" -> Other UI
    8. Choose "What data will you be accessing?" -> Application data
    9. Create service account
    10. Choose Role Project -> Editor
    11. Choose Key type JSON
    12. Press button "Continue"
    13. Rename the file to "credential.json" and save it in the visual studio project
    14. Change in Visual Studio the Copy to output Directory -> "Copy if newer"
  2. Change share config of your google calendar (Share this Calendar)

    1. Add the email address from the json file "client_email" in the google calendar config "Share with specific people"

Example Code

using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using System.IO;

namespace Test.GoogleCalendar
{
    class Program
    {
        static void Main(string[] args)
        {
            GoogleCredential credential;
            using (var stream = new FileStream("credential.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream).CreateScoped(CalendarService.Scope.Calendar);
            }

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Test Calendar Reader",
            });

            var calendars = service.CalendarList.List().Execute().Items;
            foreach (CalendarListEntry calendar in calendars)
            {
                var events = service.Events.List(calendar.Id).Execute();
            }          
        }
    }
}

Upvotes: 1

Stephan Unrau
Stephan Unrau

Reputation: 450

Here is a link to help download Calendar Client API library for .NET. The library makes OAuth and accessing the Calendar easier. See link for OAuth details but I'm pretty sure it's what you want - user logs in only once to authorize.

On same page there are sample applications using the Client API in VB.NET and MVC.

Upvotes: 0

TalonMcShay
TalonMcShay

Reputation: 316

I used something similar to the example here. What I did initially was create a simple web application using the sample logic to setup the initial auth that Google requires. I stepped through and got the Refresh Token from the state. I then saved this in an app setting that I could use later during future GetAuthorization requests. At least for what I was doing this worked well, the user never needed to authorize as long as app stayed connected.

private IAuthorizationState GetAuthorization(WebServerClient client)
    {
        IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
        string refreshToken = LoadRefreshToken();//this where I wrote code to load it from the app settings
        if (!string.IsNullOrEmpty(refreshToken))
        {
            state.RefreshToken = refreshToken;
            try
            {
                if (client.RefreshToken(state))
                    return state;
            }
            catch (ProtocolException)
            {
                return null;
            }
        }

        return null;
    }

Upvotes: 1

Related Questions