Пример #1
0
        /// <summary>
        /// This console application demonstrates all the Google Apps
        /// MultiDomain Management APIs. 
        /// </summary>
        /// <param name="args">Command-line arguments: args[0] is
        /// the primary domain, args[1] is the secondary domain,
        /// args[2] is the admin email address, args[3] is the admin 
        /// password and args[4] is test user(email address)
        /// 
        /// Example: AppsMultiDomainDemo example.com alias.com [email protected] my_password test_user_email</param>
        public static void Main(string[] args)
        {
            if (args.Length != 5)
            {
                Console.WriteLine("Syntax: AppsMultiDomainDemo <primary_domain> <secondary_domain> <admin_email> <admin_password> <testUserEmail>");
            }
            else
            {
                primaryDomain = args[0];
                secondaryDomain = args[1];
                adminEmail = args[2];
                adminPassword = args[3];
                testUserEmail = args[4];

                MultiDomainManagementService service = new MultiDomainManagementService(primaryDomain, "multidomainapis-apps-demo");
                service.setUserCredentials(adminEmail, adminPassword);

                RunSample(service);
            }
        }
        static void Main( string[] args )
        {
            //parse options
            var options = GoogleAppsAddDomainAliasProgramOptions.Parse( args );
            if( options.Help ) { options.PrintHelp( Console.Out ); return; }
            if( options.Incomplete ) { CollectOptionsInteractiveConsole( options ); }
            if( !options.Username.Contains( "@" ) )
            {
                Console.WriteLine( "Whoops. Your username must be an email address." );
                options.Username = null;
                CollectOptionsInteractiveConsole( options );
            }
            //create service object
            var service = new MultiDomainManagementService( options.Domain, null );
            service.setUserCredentials( options.Username, options.Password );

            try
            {
                //add aliases
                if( string.IsNullOrEmpty( options.File ) ) { AddAliasInteractiveConsole( service, options.Domain ); }
                else { AddAliasBatch( Console.Out, service, options.Domain, options.File ); }
            }
            catch( InvalidCredentialsException )
            {
                Console.WriteLine();
                Console.WriteLine( "Invalid Credentials." );
            }
            catch( CaptchaRequiredException )
            {
                Console.WriteLine();
                Console.WriteLine( "Your account has been locked by Google." );
                Console.WriteLine( "Use your browser to unlock your account." );
                Console.WriteLine( "https://www.google.com/accounts/UnlockCaptcha" );
            }
        }
        static void AddAliasInteractiveConsole( MultiDomainManagementService service, string primaryDomain )
        {
            Console.WriteLine();
            Console.WriteLine( "No batch file option (-f). Entering interactive mode." );
            Console.WriteLine( "Press CTRL+C to quit." );
            Console.WriteLine();
            while( true ) //continue until CTRL+C
            {
                bool confirm = false;
                string user=null, alias=null;
                while( string.IsNullOrEmpty( user ) )
                {
                    Console.Write( "User to alias [user@domain]: " );
                    user = Console.ReadLine();
                }
                while( string.IsNullOrEmpty( alias ) )
                {
                    Console.Write( String.Format( "Alias for {0}: ", user ) );
                    alias = Console.ReadLine();
                }

                Console.Write( string.Format("Please confirm: Add alias {0} to {1} (y/n)? ", alias, user) );
                confirm = Console.ReadLine().StartsWith( "y", StringComparison.InvariantCultureIgnoreCase );
                if( !confirm )
                {
                    Console.WriteLine( "Cancelled. Alias not created.");
                }
                else
                {
                    try
                    {
                        Console.WriteLine( AddAlias( service, primaryDomain, user, alias ) );
                    }
                    catch( InvalidCredentialsException )
                    {
                        Console.WriteLine();
                        Console.WriteLine( "Invalid Credentials." );
                        Console.WriteLine();
                        //collect new credentials
                        var options = new GoogleAppsAddDomainAliasProgramOptions() { Domain = primaryDomain };
                        CollectOptionsInteractiveConsole( options );
                        service.setUserCredentials( options.Username, options.Password );
                    }
                }
                Console.WriteLine();
            }
        }