Google.Api.Ads.AdWords.Examples.CSharp.v201306.HandleTwoFactorAuthorizationError.Run C# (CSharp) Method

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
return void
        public void Run(AdWordsUser user)
        {
            // Use a test account for which 2 factor authentication has been enabled.
              string loginEmail = "[email protected]";
              string password = "testaccount";

              AdWordsAppConfig config = new AdWordsAppConfig();
              config.Email = loginEmail;
              config.Password = password;
              AuthToken authToken = new AuthToken(config, "adwords");

              try {
            // Try to obtain an authToken.
            string token = authToken.GetToken();
            Console.WriteLine("Retrieved an authToken = {0} for user {1}.", token, loginEmail);
              } catch (AuthTokenException ex) {
            // Since the test account has 2 factor authentication enabled, this block
            // of code will be executed.
            if (ex.ErrorCode == AuthTokenErrorCode.BadAuthentication) {
              if (ex.Info == "InvalidSecondFactor") {
            Console.WriteLine("The user has enabled two factor authentication in this " +
                "account. Have the user generate an application-specific password to make " +
                "calls against the AdWords API. See " +
                "http://adwordsapi.blogspot.com/2011/02/authentication-changes-with-2-step.html" +
                " for more details.");
              } else {
            Console.WriteLine("Invalid credentials.");
              }
            } else {
              throw new System.ApplicationException(String.Format("The server raised an {0} error.",
              ex.ErrorCode));
            }
              }
        }

Usage Example

 /// <summary>
 /// Main method, to run this code example as a standalone application.
 /// </summary>
 /// <param name="args">The command line arguments.</param>
 public static void Main(string[] args)
 {
     HandleTwoFactorAuthorizationError codeExample = new HandleTwoFactorAuthorizationError();
       Console.WriteLine(codeExample.Description);
       try {
     codeExample.Run(new AdWordsUser());
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
HandleTwoFactorAuthorizationError