【问题标题】:.net google calendar api : anybody can create/update/delete events.net google calendar api:任何人都可以创建/更新/删除事件
【发布时间】:2016-06-06 05:46:19
【问题描述】:

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

目前我使用我的日历 ID 登录到 gmail 帐户,我能够获取不同日历 ID 的事件,但是我无法通过另一个用户日历 ID 创建/更新/删除事件。

我的要求是,我已登录到我的 gmail id,比如说 abc@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,

            });

希望您能理解我的要求。等待最早的回应。 谢谢。

【问题讨论】:

    标签: c# google-api google-calendar-api google-oauth google-api-dotnet-client


    【解决方案1】:

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

    当您使用 GoogleWebAuthorizationBroker.AuthorizeAsync 再次登录并使用 xyz@google.com 保存“User2”时,您可以访问 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();
    

    【讨论】:

    • 你能告诉我应该如何分享日历ID,或者你能给我工作示例的链接
    • 我没有手头的示例,但 developers.google.com/google-apps/calendar/v3/reference/acl/… 注意:范围是您要与之共享日历的用户电子邮件地址。
    • 从我的示例项目中为你提取了一些代码我没有测试它,虽然它应该很接近。
    • 在我与另一个日历共享我的日历后,它仅适用于共享日历但出现异常 404 未找到未共享日历获取事件的错误...你知道为什么会这样吗?请回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多