【问题标题】:Google Calendar API V3谷歌日历 API V3
【发布时间】: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)) 时收到此异常:

发送直接消息时发生协议异常错误或

得到响应。

请帮忙!!!

【问题讨论】:

标签: c# .net oauth-2.0 google-calendar-api


【解决方案1】:

您是在 Web 服务器上还是在本机应用程序中使用它?您拥有的此代码仅适用于本机应用程序,所以这可能是您的麻烦。

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多