【问题标题】:Google Calendar API in ASP.NET MVCASP.NET MVC 中的 Google 日历 API
【发布时间】:2016-03-01 02:58:40
【问题描述】:

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

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

这是我的错误:

“Google.GoogleApiException”类型的异常发生在 Google.Apis.dll 但未在用户代码中处理

补充资料:

Google.Apis.Requests.RequestError - 无效凭据 [401]

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

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

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

【问题讨论】:

  • 您的视图是什么样的?

标签: c# api model-view-controller google-calendar-api


【解决方案1】:

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

你没有发布我使用的发送范围

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

【讨论】:

  • 谢谢!这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多