OpenTween.Api.MicrosoftTranslatorApi.GetAccessTokenAsync C# (CSharp) Method

GetAccessTokenAsync() private method

private GetAccessTokenAsync ( ) : TimeSpan>>.Task
return TimeSpan>>.Task
        internal virtual async Task<Tuple<string, TimeSpan>> GetAccessTokenAsync()
        {
            var param = new Dictionary<string, string>
            {
                ["grant_type"] = "client_credentials",
                ["client_id"] = ApplicationSettings.AzureClientId,
                ["client_secret"] = ApplicationSettings.AzureClientSecret,
                ["scope"] = "http://api.microsofttranslator.com",
            };

            using (var request = new HttpRequestMessage(HttpMethod.Post, OAuthEndpoint))
            using (var postContent = new FormUrlEncodedContent(param))
            {
                request.Content = postContent;

                using (var response = await this.Http.SendAsync(request).ConfigureAwait(false))
                {
                    var responseBytes = await response.Content.ReadAsByteArrayAsync()
                        .ConfigureAwait(false);

                    return ParseOAuthCredential(responseBytes);
                }
            }
        }

Usage Example

        public async Task GetAccessTokenAsync_Test()
        {
            using (var mockHandler = new HttpMessageHandlerMock())
                using (var http = new HttpClient(mockHandler))
                {
                    var translateApi = new MicrosoftTranslatorApi(http);

                    mockHandler.Enqueue(x =>
                    {
                        Assert.Equal(HttpMethod.Post, x.Method);
                        Assert.Equal(MicrosoftTranslatorApi.IssueTokenEndpoint, x.RequestUri);

                        var keyHeader = x.Headers.First(y => y.Key == "Ocp-Apim-Subscription-Key");
                        Assert.Equal(ApplicationSettings.TranslatorSubscriptionKey, keyHeader.Value.Single());

                        return(new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(@"ACCESS_TOKEN"),
                        });
                    });

                    var result = await translateApi.GetAccessTokenAsync()
                                 .ConfigureAwait(false);

                    var expectedToken = (@"ACCESS_TOKEN", TimeSpan.FromMinutes(10));
                    Assert.Equal(expectedToken, result);

                    Assert.Equal(0, mockHandler.QueueCount);
                }
        }
All Usage Examples Of OpenTween.Api.MicrosoftTranslatorApi::GetAccessTokenAsync