Попытка создания пользователей в домене Google Apps в коде Windows Forms

Я пытаюсь создать пользователя из приложения Windows Forms.

Мне удалось сделать это в консольном приложении, но я хочу это в правильном приложении для Windows. :)

Чтобы получить правильную аутентификацию, я попытался выяснить, как работает AssertionFlowClient, но у меня ничего не вышло.

Вот критический код:

private void CreateUserBtn_Click(object sender, EventArgs e)
    {
        var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, new X509Certificate2(@"C:\key.p12", "notasecret", X509KeyStorageFlags.Exportable))
        {
            ServiceAccountId = "[email protected]",
            Scope = DirectoryService.Scopes.AdminDirectoryUser.GetStringValue(),
            ServiceAccountUser = "[email protected]",
        };

        var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

        DirectoryService m_serviceUser = new DirectoryService(new BaseClientService.Initializer()
        {
            Authenticator = auth,
        });
        User newuserbody = new User();
        UserName newusername = new UserName();
        newuserbody.PrimaryEmail = UserNameTextBox.Text + "@" + DomainTextBox.Text;
        newusername.GivenName = GivenNameTextBox.Text;
        newusername.FamilyName = FamilyNameTextBox.Text;
        newuserbody.Name = newusername;
        newuserbody.Password = PasswordTextBox.Text;

        m_serviceUser.Users.Insert(newuserbody).Execute();
    }

И вот сообщение об ошибке:

Произошло необработанное исключение типа «DotNetOpenAuth.Messaging.ProtocolException» в Google.Apis.dll.

Дополнительная информация: Произошла ошибка при отправке прямого сообщения или получении ответа.

Изменить: снова добавлен вывод отладки som:

CreateUserWinForm.vshost.exe Information: 0 : DotNetOpenAuth, Version=4.0.0.11165, Culture=neutral, PublicKeyToken=2780ccd10d57b246 (official)

CreateUserWinForm.vshost.exe Information: 0 : Preparing to send AssertionFlowMessage (2.0) message.

CreateUserWinForm.vshost.exe Information: 0 : Sending AssertionFlowMessage request.

CreateUserWinForm.vshost.exe Information: 0 : HTTP POST https://accounts.google.com/o/oauth2/token

'CreateUserWinForm.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.resources\v4.0_4.0.0.0_sv_b77a5c561934e089\System.resources.dll'

A first chance exception of type 'DotNetOpenAuth.Messaging.ProtocolException' occurred in DotNetOpenAuth.dll

Additional information: Error occurred while sending a direct message or getting the response.

A first chance exception of type 'DotNetOpenAuth.Messaging.ProtocolException' occurred in Microsoft.Threading.Tasks.dll

Additional information: Error occurred while sending a direct message or getting the response.

A first chance exception of type 'System.AggregateException' occurred in mscorlib.dll

A first chance exception of type 'DotNetOpenAuth.Messaging.ProtocolException' occurred in Google.Apis.dll

Additional information: Error occurred while sending a direct message or getting the response.

An unhandled exception of type 'DotNetOpenAuth.Messaging.ProtocolException' occurred in Google.Apis.dll

Additional information: Error occurred while sending a direct message or getting the response.

The program '[7820] CreateUserWinForm.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

person JoBe    schedule 24.07.2013    source источник
comment
Что такое сообщение об исключении и трассировка стека?   -  person SLaks    schedule 24.07.2013
comment
@SLaks - я вставил вывод отладки. Но это было очень много! Какие-нибудь подсказки новому пользователю Visual Studio 2012 Express? Как получить сообщение об исключении и трассировку стека?   -  person JoBe    schedule 24.07.2013
comment
Нажмите «Просмотреть подробности» во всплывающем окне исключения.   -  person SLaks    schedule 24.07.2013
comment
Нет подробностей просмотра, чтобы щелкнуть!   -  person JoBe    schedule 24.07.2013
comment
Что вы видите во всплывающем окне исключения?   -  person SLaks    schedule 24.07.2013
comment
Вы найдете это под кодом в моем вопросе   -  person JoBe    schedule 24.07.2013
comment
@SLaks Я добавил дополнительную информацию в свой вопрос   -  person JoBe    schedule 25.07.2013


Ответы (1)


Мне удалось решить эту проблему, прежде всего, с помощью кода modyfin из файла google-api-samples/Tasks.WinForms.NoteMgr; Вот как я это сделал:

 public static DirectoryService Service { get; private set; }

    private static IAuthenticator CreateAuthenticator()
    {
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
        provider.ClientIdentifier = <myClientId>;
        provider.ClientSecret = <myClientSecret>";
        return new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);
    }

    private static IAuthorizationState GetAuthentication(NativeApplicationClient client)
    {
        // You should use a more secure way of storing the key here as
        // .NET applications can be disassembled using a reflection tool.
        const string STORAGE = "bergstedts.directoryservice";
        const string KEY = "y},drdzf11x9;87";
        string scope = DirectoryService.Scopes.AdminDirectoryUser.GetStringValue();

        // Check if there is a cached refresh token available.
        IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
        if (state != null)
        {
            try
            {
                client.RefreshToken(state);
                return state; // Yes - we are done.
            }
            catch (DotNetOpenAuth.Messaging.ProtocolException ex)
            {
                CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
            }
        }

        // Retrieve the authorization from the user.
        state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
        AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
        return state;
    }
private void CreateUserBtn_Click(object sender, EventArgs e)
    {
        // Create Service
        DirectoryService m_serviceUser = new DirectoryService(new BaseClientService.Initializer()
        {
            Authenticator = CreateAuthenticator(),
            ApplicationName = "newuser"
        });           

        // Store data from TextBoxes in form to User-object
        User newuserbody = new User();
        newuserbody.Name = new UserName();
        newuserbody.PrimaryEmail = UserNameTextBox.Text + "@" + DomainTextBox.Text;
        newuserbody.Name.GivenName = GivenNameTextBox.Text;
        newuserbody.Name.FamilyName = FamilyNameTextBox.Text;           
        newuserbody.Password = PasswordTextBox.Text;

        // Create user and store the newly created user in ccreateduser
        User createduser = m_serviceUser.Users.Insert(newuserbody).Execute();

        // display Userdata
        CreatedUserLbl.Text = "User : " + createduser.PrimaryEmail + " has been created!";   

        // Clear inputfields (except for domain)
        UserNameTextBox.Text = "";
        GivenNameTextBox.Text = "";
        FamilyNameTextBox.Text = "";
        PasswordTextBox.Text = "";
    }
person JoBe    schedule 25.07.2013