azure 图形客户端参数异常:当前仅支持HTTP/1.0和HTTP/1.1版本请求,参数名:价值

cidc1ykv  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(136)

使用Microsoft.SDK.Graph中的GraphClient,我可以看到查询在fiddler中发出,它们都没有使用HTTP/2.0,但我得到以下错误:
参数异常:当前仅支持HTTP/1.0和HTTP/1.1版本请求。参数名:价值

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;
        }
gudnpqoy

gudnpqoy1#

我和你有同样的问题,我通过创建自定义请求到微软服务器解决了它

public static string GetAzureAccessToken(){
     var client = new RestClient("https://login.microsoftonline.com:443/your_tenantid/oauth2/v2.0/token");
     var request = new RestRequest(Method.POST);
     request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
     request.AddParameter("grant_type", "password");
     request.AddParameter("username", "your_email");
     request.AddParameter("password", "password");
     request.AddParameter("scope", "https://graph.microsoft.com/.default");
     request.AddParameter("tenant", "your_tenantid");
     request.AddParameter("client_id", "your_client_id");
     IRestResponse response = client.Execute(request);

     if(response.IsSuccessful)
     {
         var result = JsonConvert.DeserializeObject<dynamic>(response.Content);

         return result.access_token;
     }
     else
     {
         return "Fail";
     }

在获得访问令牌后,我创建了一个请求,并使用以下访问令牌向服务器发送请求:

public static string GetMailBox(string accessToken){
        var client = new RestClient("https://graph.microsoft.com/v1.0/users/"your_email"/messages?$top=10");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("Authorization", "Bearer " + accessToken);
        IRestResponse response = client.Execute(request);

        if (response.IsSuccessful)
        {

            var result = JsonConvert.DeserializeObject<dynamic>(response.Content);

            return result.value.ToString();
        }
        else
        {
            return "Fail";
        }

您可以阅读此文档,创建更多的请求,如示例GetMailBox i放在上面Microsoft Graph API document

相关问题