【问题标题】:Accessing google calendar api v3 in .net在 .net 中访问 google calendar api v3
【发布时间】:2012-05-29 07:11:36
【问题描述】:

我正在尝试将我的 asp.net (vb.net) 应用程序连接到谷歌日历,以便我可以在谷歌日历上发布活动。

到目前为止,我已经设法获得了访问令牌和刷新令牌。我计划存储刷新令牌,以便我的应用可以在用户注销很久后访问特定的谷歌日历。

那么,我已经获得了访问令牌和刷新令牌,现在呢?我找不到任何可以仅使用访问令牌连接到谷歌日历 api v3 的示例 - 所有示例,即使在谷歌网站上,都涉及使用客户端库获取访问令牌(我已经获得),并创建一个不传递参数的新 CalendarService() 对象。

是否可以创建一个 CalendarService 类并给它一个我已经拥有的访问令牌?

【问题讨论】:

    标签: .net oauth google-calendar-api


    【解决方案1】:

    花了一些时间来弄清楚 oauth 工作流程,所以我创建了一个小项目,其中包含基本概念的编码

    1. 通过 oauth 获取访问权限
    2. 刷新过期令牌
    3. 使用访问令牌做一些事情

    该项目托管在这里 https://code.google.com/p/csharp-googlecalendar-api-v3-oauth2/ 和它在 csharp 但我确实尽可能简单

    你可能想看看这个特定的文件https://code.google.com/p/csharp-googlecalendar-api-v3-oauth2/source/browse/CalChecker/simpleEvent.aspx.cs

    string accesstoken = "usersaccesstoken";
    string client_id = "getclientidfromcloud";
    string client_secret = "getclientsecretfromcould";
    string useremailaccount = "whosecal@email.com";
    
    var httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/calendar/v3/calendars/" + WebUtility.HtmlEncode(useremailaccount) + "/events");
    httpWebRequest1.ContentType = "text/json";
    httpWebRequest1.Method = "GET";
    httpWebRequest1.Headers["Authorization"] = "Bearer " + accesstoken;
    var httpResponse1 = (HttpWebResponse)httpWebRequest1.GetResponse();
    using (var streamReader = new StreamReader(httpResponse1.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
    

    所有代码都在创建一个 GET WebRequest,注意授权标头。这就是您传递访问令牌以调用需要身份验证的事件的方式。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多