4

ユーザーの Google カレンダーにアクセスするためのネイティブ アプリを作成しようとしています。認証を取得するためにGoogleが提供した例を使用しようとしていますが、認証機能を起動していないようです

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

認証に使用しているコードは次のとおりです。コンソールの書き込み行が公開されるのを見たことはありません。

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

利用可能なより良いチュートリアルはありますか、それとも何か間違っていますか?

4

3 に答える 3

2

サンプルコードは壊れています。サービスでオーセンティケーターを使用するには、接続する必要があります。この例では、サービスとオーセンティケーターの間に関連付けはありません。次のようにサービスを作成します。

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

より良いドキュメント/作業コードについては、https://code.google.com/p/google-api-dotnet-client/をご覧ください。

于 2013-04-02T19:00:05.537 に答える
0

新しいドキュメント をチェックしてください。交換する必要があります

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

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

コメントにあるように、 を呼び出すvar service = new CalendarService()と、以前に登録されたオーセンティケーターが自動的に呼び出されます。

于 2012-04-19T02:43:40.650 に答える
0

上記の方法は、古いバージョンの Google カレンダー DLL に関連していると思います。Googleカレンダーの新しいバージョン、つまりV 1.8.1.82のドキュメントについて知っている人はいますか。Google は .NET 開発者向けの優れたドキュメントを提供していません。

于 2014-05-14T12:12:36.137 に答える