简体   繁体   English

Google Calendar API V3

[英]Google Calendar API V3

I'm trying to Use Google Calendar API V3 to update my calendar. 我正在尝试使用Google Calendar API V3更新我的日历。 I want to fetch my calendar's events from C# code. 我想从C#代码中获取日历事件。 I'm using .Net Library for Google Calendar API V3. 我正在将.Net库用于Google Calendar API V3。

I'm unable to authorize my request for some reason. 由于某种原因,我无法批准我的请求。 I've tried to follow the available code samples, but in vain. 我试图遵循可用的代码示例,但徒劳。 Following is my code snippet that I'm using to authorize my request: 以下是我用来授权请求​​的代码段:

    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;
    } 

I receive this Exception when if (arg.RefreshToken(state)) is executed: if (arg.RefreshToken(state))则会收到此异常:

protocol exception Error occurred while sending a direct message or 协议异常发送直接消息时出错或

getting the response. 得到回应。

Please HELP!!! 请帮忙!!!

Are you using this on web server or in a native application? 您是在Web服务器上还是在本机应用程序中使用它? This code you have is for a native application only, so that might be your trouble. 您拥有的这段代码仅适用于本机应用程序,因此可能是您的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM