简体   繁体   中英

How do I connect to google calendar's API using Visual Studio? (C#)

I've spent much more time researching this topic than I would like to admit, but the instructions are incredibly complex to me. I'm developing a program with a calendar GUI, in which the user can select a day, and type an event he/she might have that day. Once done, I want to be able to import the user's data into a google calendar I have created. How can I do this? If you can, I would GREATLY appreciate a simplified answer, seeing as I'm a student, not a professional. Thank you!

Google has a library in .NET to connect to Calendar .

There is some documentation here , and you can find the API's documentation itself here .

You can find an example here .

I was able to follow the Google Calendar API .NET Quickstart Guide to add a Google Calendar integration to an ASP.NET MVC application.

Essentially, you will need to download an authentication file (either JSON or P12) from the Google Developer's Console and use it to create a credential:

using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
   string credPath = @"/location/to/store/credentials"
   UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
   GoogleClientSecrets.Load(stream).Secrets,
   CalendarService.Scope.Calendar,
   "user",
   CancellationToken.None,
   new FileDataStore(credPath, true)).Result;
}

With the default settings in the developer's console, you'll end up launching an OAuth 2.0 authentication pop-up each time you need permission from a new user. If they give you permission in that screen, use the credential to authorize a CalendarService object:

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

The service will then allow you to execute Create/Read/Update/Delete requests for the user you are currently authenticated for:

Event newEvent = new Event()
{
    Summary = "new event",
    Start = new EventDateTime()
    {
        DateTime = DateTime.Parse("2016-07-11T09:00:00"),
        TimeZone = "America/Los_Angeles"
    },
    End = new EventDateTime()
    {
        DateTime = DateTime.Parse("2016-07-11T10:00:00"),
        TimeZone = "America/Los_Angeles"
    }
};

Event createdEvent = service.Events.Insert(newEvent, "primary").Execute();

You can find more information about what events are available for different Google Calendar objects in the API documentation and more information on the .NET wrappers here (though the .NET examples aren't very complete; you're better off looking at SO). You can create these objects from anywhere in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM