我正在尝试使用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;
}
我正在使用它来调用我需要的Graph 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的响应,但Content-Length为-1。我现在被困在这里。有人可以帮助我解决我出错的地方/如何进一步调试这段代码。
提前致谢。
答案 0 :(得分:1)
API使用“Transfer-Encoding:chunked”,因此不返回Content-Length标头。这就是为什么你看到response.ContentLength属性的默认值为-1。要读取响应主体,只需读取response.GetResponseStream()方法获得的响应流。