代码之家  ›  专栏  ›  技术社区  ›  Swati Annaldas

.net谷歌日历api:任何人都可以创建/更新/删除事件

  •  2
  • Swati Annaldas  · 技术社区  · 8 年前

    在我的应用程序中,我使用的是谷歌日历api。 我已经为我的帐户以及少数用户创建了客户端id和客户端机密。 当我运行应用程序时,如果我没有登录到浏览器,它会要求我登录到gmail帐户。登录gmail帐户后,我就可以成功地在日历上获取/创建/更新/删除事件。

    目前我使用日历id登录gmail帐户,我可以获得不同日历id的事件,但无法通过其他用户日历id创建/更新/删除事件。

    我的要求是,我登录到我的gmail idabc@gmail.com并且我能够获取日历id的事件xyz@gmail.com但无法为日历id创建/更新/删除事件xyz@gmail.com

    我在网上做了很多研究,但什么也没发现。据我所知,我们需要向请求传递授权+访问令牌。我也试过,但没有成功。

    请建议我应该做什么来达到这个要求。

    calID = Convert.ToString(Session["CalenderId"]);
            userSecret = Convert.ToString(Session["UserSecret"]);
    
            if (!string.IsNullOrEmpty(calID) && !string.IsNullOrEmpty(userSecret))
            {
                using (var stream =
                    new FileStream(userSecret, FileMode.Open, FileAccess.Read))
                {
                    string credPath = System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.Personal);
                    credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json/" + calID);
    
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                    // Response.Write("Credential file saved to: " + credPath);
                }
    
                // Create Google Calendar API service.
                var service = new CalendarService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
    
                });
    

    希望你已经理解了我的要求。等待最早的回应。 非常感谢。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Linda Lawton - DaImTo    8 年前

    您需要记住,身份验证是基于每个用户的。当您运行GoogleWebAuthorizationBroker时。AuthorizeAsync并保存“user”登录abc@google.com您可以访问abc@googles.com的日历。

    当您再次使用GoogleWebAuthorizationBroker登录时。AuthorizeAsync并使用保存“User2”xyz@google.com您可以访问xyz@google.com的日历。

    你应该做的是xyz@google.com与共享他们的日历abc@google.com然后他们将有权更新它。

    未测试样本代码:

    AclRule body;
    body.Role = "writer";
    body.Scope.Type = "user";
    body.Scope.Value = "abc@gmail.com";
    service.Acl.Insert(body, calendarId).Execute();