gpt4 book ai didi

c# - Microsoft Graph API : HttpStatusCode 200, 内容长度-1?

转载 作者:行者123 更新时间:2023-12-03 03:09:09 24 4
gpt4 key购买 nike

我正在尝试使用 Ms Graph API 连接到 Outlook 并下载附件。到目前为止我写的是

 private static async Task<HttpWebRequest> createHttpRequestWithToken(Uri uri)
{
HttpWebRequest newRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

string clientId = "myClientId";
string clientSecret = "myClientSecret";
ClientCredential creds = new ClientCredential(clientId, clientSecret);

AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/myAzureAD/oauth2/token");
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", creds);
newRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + authResult.AccessToken);
newRequest.ContentType = "application/json";

return newRequest;
}

我用它来调用我需要的图形 API。首先,我尝试调用此 URL:

Uri uri = new Uri(("https://graph.microsoft.com/v1.0/users/myEmailId/messages"));
HttpWebRequest request = createHttpRequestWithToken(uri).Result;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

运行代码后,我收到 HttpStatusCode 200 的响应,但内容长度为 -1。我目前被困在这里。有人可以帮助我解决哪里出错/如何进一步调试这段代码吗?

提前致谢。

最佳答案

API 使用“Transfer-Encoding: chunked”,因此不会返回 Content-Length header 。这就是为什么您会看到response.ContentLength 属性的默认值为-1。要读取响应正文,只需读取通过response.GetResponseStream()方法获取的响应流即可。

关于c# - Microsoft Graph API : HttpStatusCode 200, 内容长度-1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41101300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com