Could not load type 'Google.Apis.Requests.IErr

2019-07-25 17:31发布

问题:

I am Google Auth api Calendar in .net. I have copied the code from this link:Refresh Token Expiration time Google calendar Could not load type 'Google.Apis.Requests.IErrorResponseHandler' from assembly 'Google.Apis, Version=1.6.0.16897, Culture=neutral, PublicKeyToken=null'

Here is my code:

private CalendarService CreateService(string token)
    {
        //KeyValuePair<string, string> credentials = Ninject.Web.Common.Get3LOCredentials();
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, this.clientID, this.ClientSecret);
        //provider.ClientIdentifier = clientID;
        //provider.ClientSecret = clientSecret;
        var auth = new Google.Apis.Authentication.OAuth2.OAuth2Authenticator<NativeApplicationClient>(provider, (p) => GetAuthorization(provider, token));
        CalendarService service = new CalendarService(new BaseClientService.Initializer()
        {
            Authenticator = auth,
            ApiKey = ConfigurationManager.AppSettings[this.APIkey].ToString(),
            GZipEnabled = false
        });
        return service;
    }

    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg, String Refreshtoken)
    {
        IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
        state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
        state.RefreshToken = Refreshtoken;
        return state;
    }

回答1:

Did you use package manager to install the API?

Further onwards, Are you going to request access everytime or do you want to save the refreshtoken so that you can get a non-expired access token when you make following calls?



回答2:

I solved this problem by uninstalling and re-installing the NuGet package "google.apis.auth". This was because multiple versions are installed.

  1. Right click on the "References" menu of the project. Manually remove all package references related to google.apis.auth.

  2. Then, select "Manage NuGet Package", search for "google.apis.auth", and uninstall "google.apis.auth".

  3. Reinstall the same package.

  4. Clean and rebuild

If it doesn't work, you might try reinstall "google.apis" as well.