예제 #1
0
        /// <summary>
        /// Does the request.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns>Response deserialized as an instance of T.</returns>
        /// <exception cref="FulfillmentException">Token expired. Please logout and login again.</exception>
        public async Task <T> DoRequest(string url, string method, Dictionary <string, object> parameters, Dictionary <string, object> headers = null, string contentType = "application/json")
        {
            try
            {
                this.logger?.Info("Call Rest Service : {0}" + JsonSerializer.Serialize(new { url = url, method = method, parameters = parameters, headers = headers, contentType = contentType }));

                var accessTokenResult = await ADAuthenticationHelper.GetAccessToken(this.clientConfiguration).ConfigureAwait(false);

                if (headers == null)
                {
                    headers = new Dictionary <string, object>();
                }

                // Add bearer token
                headers.Add("Authorization", string.Format($"Bearer {accessTokenResult.AccessToken}"));

                var webRequestHelper = new WebRequestHelper(url, method, contentType);
                await webRequestHelper.PrepareDataForRequest(parameters)
                .FillHeaders(headers)
                .DoRequestAsync().ConfigureAwait(false);

                return(await webRequestHelper.BuildResultFromResponse <T>().ConfigureAwait(false));
            }
            catch (WebException ex)
            {
                this.logger?.Error(string.Format($"An error occurred while processing request to the url : {url} - {ex.ToString()}"));
                return(this.ProcessErrorResponse(url, ex));
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the access token.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <returns>Get Authentication Token.</returns>
        public static async Task <ADAuthenticationResult> GetAccessToken(SaaSApiClientConfiguration settings)
        {
            string authorizeUrl     = string.Format($"https://login.microsoftonline.com/{settings.TenantId}/oauth2/token");
            var    webRequestHelper = new WebRequestHelper(authorizeUrl, HttpMethods.POST, "application/x-www-form-urlencoded");

            var payload = new Dictionary <string, object>();

            payload.Add("Grant_type", "client_credentials");
            payload.Add("Client_id", settings.ClientId);
            payload.Add("Client_secret", settings.ClientSecret);
            payload.Add("Resource", settings.Resource);

            await webRequestHelper.PrepareDataForRequest(payload).DoRequestAsync().ConfigureAwait(false);

            return(await webRequestHelper.BuildResultFromResponse <ADAuthenticationResult>());
        }