Search code examples
c#azuremicrosoft-graph-sdks

Graphclient ArgumentException: Only HTTP/1.0 and HTTP/1.1 version requests are currently supported. Parameter name: value


Using GraphClient from Microsoft.SDK.Graph I can see the queries are going out in fiddler, none of them use HTTP/2.0 but I get this error:

ArgumentException: Only HTTP/1.0 and HTTP/1.1 version requests are currently supported. Parameter name: value

private async Task<UserAccount> FetchAzurePropsFromGraph()
        {
            var scopes = new[] { "User.Read" };
            var clientId = EWSMailboxSyncProvider.CLIENT_ID;
            var tenantId = ServicesConfiguration.GetStoredTenantID();
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            var transport = new Azure.Core.Pipeline.HttpClientTransport(client);
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
                Transport = transport
            };

            var userNamePasswordCredential = new UsernamePasswordCredential(
                    _o365UserName, _o365Password, tenantId, clientId, options);
            var graphClient = new Microsoft.Graph.GraphServiceClient(userNamePasswordCredential, scopes);
            var user = await graphClient.Users[_emailAddress].Request().GetAsync();
            var result = new UserAccount();
            if (user != null)
            {
                result.DisplayName = user.DisplayName;
                result.City = user.City;
                result.Company = user.CompanyName;
                result.DisabledAccount = !(user.AccountEnabled ?? true);
                result.DistinguishedName = user.OnPremisesDistinguishedName;
                result.DomainName = user.OnPremisesDomainName;
                result.EmailAddresses.Add(user.Mail);
                foreach (var mail in user.OtherMails)
                {
                    result.EmailAddresses.Add(mail);
                }
            }
            return result;
        }

Solution

  • Using the Graph SDK GraphClientFactory.Create method, you can update to the latest packages for .Net Framework 4.8 and get a HttpClient object that you can use without having to create a custom request and the graph call will not throw that error:

    HttpClient httpClient = GraphClientFactory.Create();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
    GraphServiceClient graphClient = new GraphServiceClient(httpClient);
    var users = await graphClient.Users.GetAsync(requestConfig =>
    {
        requestConfig.QueryParameters.Filter =
            "startsWith(givenName, '" + match + "') or startsWith(surname, '" + match + "') or startsWith(mail, '" + match + "')";
    });
    return users.Value;
    

    See https://learn.microsoft.com/en-us/dotnet/api/microsoft.graph.graphclientfactory.create?view=graph-core-dotnet for more about that method ands its optional parameters.