/**
         * Creating a new file that contains the envelopeId and orderNumber
         */
        private static void saveDoc(string envelopeId, string orderNumber)
        {
            try
            {
                ApiClient dsApiClient = new ApiClient();
                JWTAuth   dsJWTAuth   = new JWTAuth(dsApiClient);
                // Checks for the token before calling the function getToken
                dsJWTAuth.CheckToken();
                var config = new Configuration(new ApiClient(dsJWTAuth.getBasePath()));
                config.AddDefaultHeader("Authorization", "Bearer " + dsJWTAuth.getToken());
                EnvelopesApi envelopesApi = new EnvelopesApi(config);

                // Create the output directory if needed
                string outputPath = Path.Combine(mainPath, "output");

                if (!Directory.Exists(outputPath))
                {
                    DirectoryInfo directory = Directory.CreateDirectory(outputPath);
                    if (!directory.Exists)
                    {
                        throw new Exception(DateTime.Now + " Failed to create directory.");
                    }
                }

                Stream results  = envelopesApi.GetDocument(dsJWTAuth.getAccountID(), envelopeId, "combined");
                string filePath = Path.Combine(mainPath, Path.Combine("output", DSConfig.FilePrefix + orderNumber + ".pdf"));
                // Create the output file
                using (System.IO.Stream stream = File.Create(filePath))
                    results.CopyTo(stream);

                if (!File.Exists(filePath))
                {
                    throw new Exception(DateTime.Now + " Failed to create file");
                }

                // Catch exception if BREAK_TEST equals to true or if orderNumber contains "/break"
                if (DSConfig.EnableBreakTest.Equals("true") && ("" + orderNumber).Contains("/break"))
                {
                    throw new Exception(DateTime.Now + " Break test");
                }
            }
            catch (ApiException e)
            {
                Console.WriteLine(DateTime.Now + " API exception: " + e.Message);
                throw new Exception(DateTime.Now + " saveDoc error");
            }

            // Catch exception while trying to save the document
            catch (Exception e)
            {
                Console.WriteLine(DateTime.Now + " Error while fetching and saving docs for envelope " + envelopeId + ", order " + orderNumber);
                Console.WriteLine(e.Message);
                throw new Exception(DateTime.Now + " saveDoc error");
            }
        }
예제 #2
0
        /**
         * Check that we can get a DocuSign token and handle common error
         * cases: ds_configuration not configured, need consent.
         */
        private static void testToken()
        {
            try
            {
                if (String.Equals(DSConfig.ClientID, "{CLIENT_ID}"))
                {
                    Console.WriteLine("Problem: you need to configure this example, either via environment variables (recommended)");
                    Console.WriteLine("or via the ds_configuration.js file.");
                    Console.WriteLine("See the README file for more information\n");
                    return;
                }

                JWTAuth dsJWTAuth = new JWTAuth(apiClient);
                dsJWTAuth.CheckToken();
            }
            // An API problem
            catch (ApiException e)
            {
                Console.WriteLine("\nDocuSign Exception!");

                // Special handling for consent_required
                String message = e.Message;
                if (!String.IsNullOrWhiteSpace(message) && message.Contains("consent_required"))
                {
                    String consent_url = String.Format("\n    {0}/oauth/auth?response_type=code&scope={1}&client_id={2}&redirect_uri={3}",
                                                       DSConfig.AuthenticationURL, DSConfig.PermissionScopes, DSConfig.ClientID, DSConfig.OAuthRedirectURI);

                    Console.WriteLine("C O N S E N T   R E Q U I R E D");
                    Console.WriteLine("Ask the user who will be impersonated to run the following url: ");
                    Console.WriteLine(consent_url);
                    Console.WriteLine("\nIt will ask the user to login and to approve access by your application.");
                    Console.WriteLine("Alternatively, an Administrator can use Organization Administration to");
                    Console.WriteLine("pre-approve one or more users.");
                    Environment.Exit(0);
                }
                else
                {
                    // Some other DocuSign API problem
                    Console.WriteLine("    Reason: {0}", e.ErrorCode, " ", e.Message);
                    Console.WriteLine("    Error Reponse: {0}", e.ErrorContent);
                    Environment.Exit(0);
                }
            }
            // Not an API problem
            catch (Exception e)
            {
                Console.WriteLine(DateTime.Now + " " + e.Message);
            }
        }