2012-05-29 115 views
1

我试图连接我的asp.net(vb.net)应用谷歌日历,所以我可以张贴在谷歌日历事件。访问谷歌日历API V3在.NET

到目前为止,我已经成功地获得访问令牌和刷新令牌。我计划存储刷新令牌,以便我的应用可以在用户注销很长时间后访问特定的Google日历。

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

是否有可能创建一个CalendarService类,并给它一个访问令牌我已经有了?

回答

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 = "[email protected]"; 

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,注意授权头..这是你如何通过访问令牌调用的事件需要身份验证。

希望它有帮助