2

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 !!

1
  • I found a lot of answers with incomplete information or wrong information. The Google quick start example doesn't address caching credentials at all. Tino's answer below is excellent. I highly recommend it. Commented Jul 24, 2019 at 16:34

3 Answers 3

1

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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ! I will check that !
Now I have this error in a web page: Missing required parameter: scope
I found my old web app project. Here is the code behind that I used to save the RefreshToken. Hopefully it will be helpful.
1

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();
            }          
        }
    }
}

1 Comment

This worked very well. Navigating the Google consoles as you describe is a bit tricky, but I'm pretty sure that's because they change regularly. For anyone else looking to get this to work: Don't forget that last step to share the calendar with the email address in the credentials.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.