简体   繁体   中英

Google Calendar API V3

I'm trying to Use Google Calendar API V3 to update my calendar. I want to fetch my calendar's events from C# code. I'm using .Net Library for 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:

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? This code you have is for a native application only, so that might be your trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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