public async Task <AccountSession> SendTokenRequestAsync(string requestBodyString, IHttpProvider httpProvider) { var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, OAuthConstants.MicrosoftAccountTokenServiceUrl); httpRequestMessage.Content = new StringContent(requestBodyString, Encoding.UTF8, "application/x-www-form-urlencoded"); using (var authResponse = await httpProvider.SendAsync(httpRequestMessage).ConfigureAwait(false)) using (var responseStream = await authResponse.Content.ReadAsStreamAsync().ConfigureAwait(false)) { var responseValues = httpProvider.Serializer.DeserializeObject <IDictionary <string, string> >( responseStream); if (responseValues != null) { OAuthErrorHandler.ThrowIfError(responseValues); return(new AccountSession(responseValues)); } throw new ServiceException( new Error { Code = OAuthConstants.ErrorCodes.AuthenticationFailure, Message = "Authentication failed. No response values returned from authentication flow." }); } }
protected async Task <BusinessServiceInformation> RetrieveMyFilesServiceResourceAsync(IHttpProvider httpProvider) { using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, OAuthConstants.ActiveDirectoryDiscoveryServiceUrl)) { await this.authenticationProvider.AuthenticateRequestAsync(httpRequestMessage).ConfigureAwait(false); using (var response = await httpProvider.SendAsync(httpRequestMessage).ConfigureAwait(false)) using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { var responseValues = httpProvider.Serializer.DeserializeObject <DiscoveryServiceResponse>(responseStream); if (responseValues == null || responseValues.Value == null) { throw new ServiceException( new Error { Code = OAuthConstants.ErrorCodes.AuthenticationFailure, Message = "MyFiles capability not found for the current user." }); } var service = responseValues.Value.FirstOrDefault(value => string.Equals(value.ServiceApiVersion, "v2.0", StringComparison.OrdinalIgnoreCase) && string.Equals(value.Capability, "MyFiles", StringComparison.OrdinalIgnoreCase)); if (service == null) { throw new ServiceException( new Error { Code = OAuthConstants.ErrorCodes.AuthenticationFailure, Message = "MyFiles capability with version v2.0 not found for the current user.", }); } return(new BusinessServiceInformation { ServiceEndpointBaseUrl = service.ServiceEndpointUri, ServiceResourceId = service.ServiceResourceId, }); } } }