【发布时间】:2012-11-14 04:33:35
【问题描述】:
我正在尝试使用 Google Calendar API V3 更新我的日历。我想从 C# 代码中获取我的日历事件。我正在为 Google Calendar API V3 使用 .Net 库。
由于某种原因,我无法授权我的请求。我试图遵循可用的代码示例,但徒劳无功。以下是我用来授权我的请求的代码 sn-p:
private void GetEvents()
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description,MyClientId, MySecurityId);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
CalendarService myService = new CalendarService(auth);
try
{
Events result = myService.Events.List(MyCalendarId).Fetch();
if (result.Items.Count > 0)
{
}
}
catch (Google.GoogleApiRequestException ex)
{
//throw ex;
}
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[] { "https://www.googleapis.com/auth/calendar.readonly" });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
if (!string.IsNullOrEmpty(refreshToken)) // refreshToken you stored in step 4
{
try
{
state.RefreshToken = refreshToken;
if (arg.RefreshToken(state)) // This is calling out to the OAuth servers with the refresh token getting back a session token, returns true if successful.
{
if (state.RefreshToken != refreshToken) // if the refresh token has changed, save it.
{
//PersistRefreshToken(authorization.RefreshToken);
}
return state; // Retain the authorization state, this is what will authenticate your calls.
}
}
catch (ProtocolException ex) { throw ex; }
}
return state;
}
在执行if (arg.RefreshToken(state)) 时收到此异常:
发送直接消息时发生协议异常错误或
得到响应。
请帮忙!!!
【问题讨论】:
-
看看这篇文章,看看它是否对你有帮助。 stackoverflow.com/questions/8899000/…
标签: c# .net oauth-2.0 google-calendar-api