示例#1
0
        public void OAuthAuthorizationCodeFlowTest()
        {
            // Make an API call with the token
            ApiClient apiClient = new ApiClient(BaseUrl);

            DocuSign.eSign.Client.Configuration.Default.ApiClient = apiClient;

            // Initiate the browser session to the Authentication server
            // so the user can login.
            string accountServerAuthUrl = apiClient.GetAuthorizationUri(client_id, redirect_url, true, stateOptional);

            System.Diagnostics.Process.Start(accountServerAuthUrl);

            WaitForCallbackEvent = new ManualResetEvent(false);

            // Launch a self-hosted web server to accepte the redirect_url call
            // after the user finishes authentication.
            using (WebApp.Start <Startup>("http://localhost:3000"))
            {
                Trace.WriteLine("WebServer Running. Waiting for access_token...");

                // This waits for the redirect_url to be received in the REST controller
                // (see classes below) and then sleeps a short time to allow the response
                // to be returned to the web browser before the server session ends.
                WaitForCallbackEvent.WaitOne(60000, false);
                Thread.Sleep(1000);
            }
            Assert.IsNotNull(AccessCode);

            string accessToken = apiClient.GetOAuthToken(client_id, client_secret, true, AccessCode);

            Assert.IsNotNull(accessToken);
            Trace.WriteLine("Access_token: " + accessToken);

            // we will retrieve this from the login API call
            string accountId = null;

            /////////////////////////////////////////////////////////////////
            // STEP 1: LOGIN API
            /////////////////////////////////////////////////////////////////

            // login call is available in the authentication api
            AuthenticationApi authApi   = new AuthenticationApi();
            LoginInformation  loginInfo = authApi.Login();

            // parse the first account ID that is returned (user might belong to multiple accounts)
            accountId = loginInfo.LoginAccounts[0].AccountId;

            // Update ApiClient with the new base url from login call
            apiClient = new ApiClient(loginInfo.LoginAccounts[0].BaseUrl);

            /////////////////////////////////////////////////////////////////
            // STEP 2: CREATE ACCOUNTS API
            /////////////////////////////////////////////////////////////////
            AccountsApi        accountsApi        = new AccountsApi();
            AccountInformation accountInformation = accountsApi.GetAccountInformation(accountId);

            Trace.WriteLine(accountInformation.ToString());
        }
        public void GetAccountInformation_CorrectAccountId_ReturnAccountInformation()
        {
            AccountInformation accountInformation = _accountsApi.GetAccountInformation(_testConfig.AccountId);

            Assert.IsNotNull(accountInformation?.AccountIdGuid);
            Assert.IsNotNull(accountInformation?.AccountName);
            Assert.IsNotNull(accountInformation?.BillingProfile);
        }
示例#3
0
        public void OAuthAuthorizationCodeFlowTest()
        {
            // Initiate the browser session to the Authentication server
            // so the user can login.
            string accountServerAuthUrl = string.Format("https://{0}/oauth/auth?response_type=code&scope=all&client_id={1}&redirect_uri={2}&state=testState",
                                                        AccountServerHost,
                                                        client_id,
                                                        redirect_url,
                                                        stateOptional);

            System.Diagnostics.Process.Start(accountServerAuthUrl);

            WaitForCallbackEvent = new ManualResetEvent(false);

            // Launch a self-hosted web server to accepte the redirect_url call
            // after the user finishes authencation.
            using (WebApp.Start <Startup>("http://localhost:8090"))
            {
                Trace.WriteLine("WebServer Running- Waiting for access_token");

                // This waits for the redirect_url to be received in the REST controller
                // (see classes below) and then sleeps a short time to allow the response
                // to be returned to the web browser before the server session ends.
                WaitForCallbackEvent.WaitOne(60000, false);
                Thread.Sleep(1000);
            }

            Assert.IsNotNull(AccessCode);

            // The Authentication is completed, so now echange a code returned for
            // the access_token and refresh_token
            var webClient = new WebClient();

            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            // Add the Authorization header with client_id and client_secret as base64
            string codeAuth = client_id + ":" + client_secret;

            byte[] codeAuthBytes  = Encoding.UTF8.GetBytes(codeAuth);
            string codeAuthBase64 = Convert.ToBase64String(codeAuthBytes);

            webClient.Headers.Add("Authorization", "Basic " + codeAuthBase64);

            // Add the code returned from the Authentication site
            string tokenGrantAndCode = string.Format("grant_type=authorization_code&code={0}", AccessCode);

            // Call the token endpoint to exchange the code for an access_token
            string        tokenEndpoint = string.Format("https://{0}/oauth/token", AccountServerHost);
            string        tokenResponse = webClient.UploadString(tokenEndpoint, tokenGrantAndCode);
            TokenResponse tokenObj      = JsonConvert.DeserializeObject <TokenResponse>(tokenResponse);

            Assert.IsNotNull(tokenObj);
            Assert.IsNotNull(tokenObj.access_token);
            Trace.WriteLine("Access_token: " + tokenObj.access_token);

            // Make an API call with the token
            ApiClient apiClient = new ApiClient(BaseUrl);

            DocuSign.eSign.Client.Configuration.Default.ApiClient = apiClient;
            DocuSign.eSign.Client.Configuration.Default.AddDefaultHeader("Authorization", "Bearer " + tokenObj.access_token);

            AccountsApi        accountsApi        = new AccountsApi();
            AccountInformation accountInformation = accountsApi.GetAccountInformation("1");

            Trace.WriteLine(accountInformation.ToString());

            // Generally the refresh token is stored away and used to get a new access_token without authenticating via the browser
            // when the access_token expires (see expires_in). Here we test that the refresh_token can be
            // exchanged for a new access_token

            webClient = new WebClient();
            webClient.Headers.Add("Authorization", "Basic " + codeAuthBase64);
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            // Add the code returned from the Authentication site
            string refreshGrant = string.Format("grant_type=refresh_token&refresh_token={0}", tokenObj.refresh_token);

            tokenResponse = webClient.UploadString(tokenEndpoint, refreshGrant);
            tokenObj      = JsonConvert.DeserializeObject <TokenResponse>(tokenResponse);

            Assert.IsNotNull(tokenObj);
            Assert.IsNotNull(tokenObj.access_token);
            Trace.WriteLine("Access_token (After Refresh): " + tokenObj.access_token);

            // Try another call with new acccess token
            accountInformation = accountsApi.GetAccountInformation("1");
            Trace.WriteLine(accountInformation.ToString());
        }