예제 #1
0
        private static async Task <AuthenticationHeaderValue> GetAuthorizationHeader(
            PowerofficeApiSettings apiSettings,
            Guid clientKey)
        {
            using (var authenticationClient = new HttpClient())
            {
                authenticationClient.BaseAddress = apiSettings.AuthenticationBaseAddress;

                var tokensRequest = new HttpRequestMessage(HttpMethod.Post, $"{apiSettings.AuthenticationBaseAddress}OAuth/Token");
                tokensRequest.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
                string encodedTokens = StringUtilities.Base64Encode($"{apiSettings.ApplicationKey}:{clientKey}");
                tokensRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", encodedTokens);

                var tokensResponse = await authenticationClient.SendAsync(tokensRequest);

                if (!tokensResponse.IsSuccessStatusCode)
                {
                    string responseInfo = await GetResponseInfo(tokensResponse);

                    throw new ApplicationException($"Could not get access token. {responseInfo}");
                }

                var tokens = JsonConvert.DeserializeObject <AuthorizationTokens>(await tokensResponse.Content.ReadAsStringAsync());
                var authorizationHeader = new AuthenticationHeaderValue(tokens.TokenType, tokens.AccessToken);
                return(authorizationHeader);
            }
        }
예제 #2
0
        internal static async Task <PowerofficeClient> Create(
            PowerofficeApiSettings apiSettings,
            Guid clientKey)
        {
            var authorizationHeader = await GetAuthorizationHeader(apiSettings, clientKey);

            var powerofficeClient = new PowerofficeClient(authorizationHeader);

            lock (SetBaseAddressLock)
            {
                if (HttpClient.BaseAddress == null)
                {
                    HttpClient.BaseAddress = apiSettings.ApiBaseAddress;
                }
            }

            return(powerofficeClient);
        }
 public PowerofficeClientFactory(PowerofficeApiSettings apiSettings)
 {
     ApiSettings = apiSettings;
 }