Google ASP.NET MVC 中的日历 API

Google Calendar API in ASP.NET MVC

所以,我花了很多时间在谷歌上搜索这个问题。基本上,这是我的授权码:

        UserCredential credential;

        using (var stream =
            new FileStream(@"C:\Users\Marto\Desktop\project\MVC_2b\App_Data\client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        }

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

这就是错误发生的地方:(Execute() 上的错误 401)

Calendar calendar=new Calendar();
        calendar.Id = "Test";
        service.Calendars.Insert(calendar).Execute();

这是我的错误:

An exception of type 'Google.GoogleApiException' occurred in Google.Apis.dll but was not handled in user code

附加信息:

Google.Apis.Requests.RequestError - Invalid Credentials [401]

Errors [Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]]

我不确定我是否没有在授权的情况下做正确的事情。

任何帮助将不胜感激,谢谢!

嗯,这有点痛苦,但我成功了。

你没有post你发送我使用的是什么范围

CalendarService.Scope.Calendar

构建新日历

var NewCalendar = new Google.Apis.Calendar.v3.Data.Calendar();
NewCalendar.Summary = "My New Calendar";   // this is the title of the new calendar

注意你不能发送 ID 这是 Google 创建的东西

发送请求

var request = service.Calendars.Insert(NewCalendar );              
var myCalendar = request.Execute();