Exemplo n.º 1
0
        public async Task <string> GetAccessTokenAsync(string appId, string appSecret)
        {
            if (string.IsNullOrWhiteSpace(appId))
            {
                throw new ArgumentNullException(nameof(appId));
            }

            if (string.IsNullOrWhiteSpace(appSecret))
            {
                throw new ArgumentNullException(nameof(appSecret));
            }

            using (var client = _httpClientFactory())
            {
                var uri = new Uri($"https://graph.facebook.com/oauth/access_token?client_id={appId}&client_secret={appSecret}&grant_type=client_credentials");

                var response = await client.GetAsync(uri);

                if (!response.IsSuccessStatusCode)
                {
                    throw new FacebookApiException("Http client response was not successful.");
                }

                var content = await response.Content.ReadAsStringAsync();

                if (string.IsNullOrWhiteSpace(content))
                {
                    throw new FacebookApiException("Http client response was empty.");
                }

                var tokenObject = _jsonParser.ParseJsonResponse <FacebookAccessToken>(content);
                if (tokenObject == null)
                {
                    throw new FacebookApiException("Token parsing failed.");
                }

                return(tokenObject.AccessToken);
            }
        }
Exemplo n.º 2
0
        public void GetAccessToken_ShouldThrowFacebookApiException_WhenClientReponseParsingReturnsNull()
        {
            // Arrange
            _httpMockHandler.When("*")
            .Respond(HttpStatusCode.OK, "application/json", "content");
            _jsonParser.ParseJsonResponse <FacebookAccessToken>(Arg.Any <string>())
            .Returns(null as FacebookAccessToken);
            var wrapper = new FacebookCommentsApiWrapper(_httpClientFactory, _jsonParser);

            // Act
            Func <Task> action = async() => await wrapper.GetAccessTokenAsync("appId", "appSecret");

            // Assert
            action.ShouldThrow <FacebookApiException>()
            .And.Message.Should().Be("Token parsing failed.");
        }