Google Authentication Process

荒凉一梦 提交于 2019-12-07 08:57:04

问题


I am trying to write a native app to access a users google calendar. I am trying to use the example that google has provided to get authentication but it never seems to fire the authentication function

private void Window_Initialized(object sender, EventArgs e)
{
    var provider = new NativeApplicationClient(
            GoogleAuthenticationServer.Description);
    provider.ClientIdentifier = "<My Client Id here>";
    provider.ClientSecret = "<My Client Secret here";

    var auth = new OAuth2Authenticator<NativeApplicationClient>(
        provider, (p) => GetAuthorization(provider));

    CalendarService service = new CalendarService();
    CalendarsResource.GetRequest cr = service.Calendars.Get("{primary}");

    if (cr.CalendarId != null)
    {
        Console.WriteLine("Fetching calendar");
        //Google.Apis.Calendar.v3.Data.Calendar c =
            service.Calendars.Get("{primary}").Fetch();
    }
    else
    {
        Console.WriteLine("Service not found");
    }
}

Here is the code that I am using for Authentication. I never see the console writeline get published.

private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
    Console.WriteLine("Authorization Requested");

    IAuthorizationState state = new AuthorizationState(
        new[] { CalendarService.Scopes.Calendar.GetStringValue() });
    state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
    Uri authUri = arg.RequestUserAuthorization(state);

    Process.Start(authUri.ToString());
    // Request authorization from the user and get the code
    string authCode = Console.ReadLine();

    // Retrieve the access token by using the authorization code:
    return arg.ProcessUserAuthorization(authCode, state);
}

Are there any better tutorials available or am I doing something wrong?


回答1:


The example code is broken. To make the service use your authenticator, you need to connect it. In the example there is no association between the service and the authenticator. Create the service like this:

var service = new CalendarService(new BaseClientService.Initializer()
{
    Authenticator = auth
};

Look at https://code.google.com/p/google-api-dotnet-client/ for better documentation/working code.




回答2:


Check out the new docs. You'll need to replace

var auth = new OAuth2Authenticator<NativeApplicationClient>(
    provider, (p) => GetAuthorization(provider));

with

AuthenticatorFactory.GetInstance().RegisterAuthenticator(
    () => new OAuth2Authenticator(provider, GetAuthentication));

As the comment says, when you call var service = new CalendarService(), the previously registered authenticator will automatically be called.




回答3:


I think the above ways are related to old version of Google calendar DLL. Does any one know about the documentation of new version of Google calendar i.e. V 1.8.1.82. Google never provides the good documentation for .NET developer.s



来源:https://stackoverflow.com/questions/9792187/google-authentication-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!