public void OAuthAuthorizationSessionTest()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                const string oAuthType         = "AzureDataLake";
                string       resourceGroupName = TestUtilities.GenerateName("resourcegroup");
                string       factoryName       = TestUtilities.GenerateName("DataFactory");
                string       serverLocation    = TestHelper.GetDefaultLocation();

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client         = TestHelper.GetDataFactoryManagementClient(handler);

                ResourceGroup resourceGroup = new ResourceGroup()
                {
                    Location = serverLocation
                };
                resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);

                // create a data factory
                var df = new Microsoft.Azure.Management.DataFactories.Models.DataFactory()
                {
                    Name     = factoryName,
                    Location = serverLocation
                };

                client.DataFactories.CreateOrUpdate(resourceGroupName, new DataFactoryCreateOrUpdateParameters()
                {
                    DataFactory = df,
                });

                AuthorizationSessionGetResponse response = client.OAuth.Get(resourceGroupName, factoryName, oAuthType);

                Assert.True(response.StatusCode == HttpStatusCode.OK);
                Assert.NotNull(response.AuthorizationSession.Endpoint);
                Assert.NotNull(response.AuthorizationSession.SessionId);
            }
        }
        /// <summary>
        /// Gets an OAuth authorization session.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The resource group name of the data factory.
        /// </param>
        /// <param name='dataFactoryName'>
        /// Required. A unique data factory instance name.
        /// </param>
        /// <param name='linkedServiceType'>
        /// Required. The type of OAuth linked service.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The Get authorization session operation response.
        /// </returns>
        public async Task <AuthorizationSessionGetResponse> GetAsync(string resourceGroupName, string dataFactoryName, string linkedServiceType, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (resourceGroupName != null && resourceGroupName.Length > 1000)
            {
                throw new ArgumentOutOfRangeException("resourceGroupName");
            }
            if (Regex.IsMatch(resourceGroupName, "^[-\\w\\._\\(\\)]+$") == false)
            {
                throw new ArgumentOutOfRangeException("resourceGroupName");
            }
            if (dataFactoryName == null)
            {
                throw new ArgumentNullException("dataFactoryName");
            }
            if (dataFactoryName != null && dataFactoryName.Length > 63)
            {
                throw new ArgumentOutOfRangeException("dataFactoryName");
            }
            if (Regex.IsMatch(dataFactoryName, "^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$") == false)
            {
                throw new ArgumentOutOfRangeException("dataFactoryName");
            }
            if (linkedServiceType == null)
            {
                throw new ArgumentNullException("linkedServiceType");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("dataFactoryName", dataFactoryName);
                tracingParameters.Add("linkedServiceType", linkedServiceType);
                TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourcegroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/Microsoft.DataFactory/datafactories/";
            url = url + Uri.EscapeDataString(dataFactoryName);
            url = url + "/oauth/authorizationsession/";
            url = url + Uri.EscapeDataString(linkedServiceType);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-09-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-client-request-id", Guid.NewGuid().ToString());

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    AuthorizationSessionGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new AuthorizationSessionGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            AuthorizationSession authorizationSessionInstance = new AuthorizationSession();
                            result.AuthorizationSession = authorizationSessionInstance;

                            JToken endpointValue = responseDoc["endpoint"];
                            if (endpointValue != null && endpointValue.Type != JTokenType.Null)
                            {
                                Uri endpointInstance = TypeConversion.TryParseUri(((string)endpointValue));
                                authorizationSessionInstance.Endpoint = endpointInstance;
                            }

                            JToken sessionIdValue = responseDoc["sessionId"];
                            if (sessionIdValue != null && sessionIdValue.Type != JTokenType.Null)
                            {
                                string sessionIdInstance = ((string)sessionIdValue);
                                authorizationSessionInstance.SessionId = sessionIdInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }