C# Office 365图形获取Microsoft团队邮件

C# Office 365图形获取Microsoft团队邮件,c#,api,office365,microsoft-graph-api,C#,Api,Office365,Microsoft Graph Api,使用Office365图表获取团队消息时遇到问题 我使用 string signedInUserID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; if (HttpContext.Current.Application.Count > 0) { signedInUserID = HttpContext.Curren

使用Office365图表获取团队消息时遇到问题

我使用

string signedInUserID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
if (HttpContext.Current.Application.Count > 0)
{
    signedInUserID = HttpContext.Current.Application.Keys[0].Replace("_TokenCache", "");
}
HttpContextBase httpContextBase = HttpContext.Current.GetOwinContext().Environment["System.Web.HttpContextBase"] as HttpContextBase;

ADALTokenCache tokenCache = new ADALTokenCache(signedInUserID);
var cachedItems = tokenCache.ReadItems(); // see what's in the cache

AuthenticationContext authContext = new AuthenticationContext(Authority, tokenCache);
ClientCredential clientCredential = new ClientCredential(clientId, appKey);
string userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
UserIdentifier userId = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);
try
{
    AuthenticationResult result = await authContext.AcquireTokenAsync(graphResourceID2, clientCredential).ConfigureAwait(false);
    return result.AccessToken;
}
然后,为了获取团队消息,我将其用作endoint:

使用从上面接收的访问令牌和端点,我执行以下操作:

using (var client = new HttpClient())
{
    using (var request = new HttpRequestMessage(HttpMethod.Get, endpoint))
    {
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        using (var response = await client.SendAsync(request).ConfigureAwait(false))
        {
            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                var result = JsonConvert.DeserializeObject<GraphOdataResponse>(json);
                myTeamsMessages = result.messages.ToList();
            }

            return myTeamsMessages;
        }
    }
}
使用(var-client=new-HttpClient())
{
使用(var request=newhttprequestmessage(HttpMethod.Get,endpoint))
{
request.Headers.Accept.Add(新的MediaTypeWithQualityHeaderValue(“application/json”);
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,accessToken);
使用(var response=await client.sendaync(request.ConfigureAwait(false))
{
if(响应。IsSuccessStatusCode)
{
var json=await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var result=JsonConvert.DeserializeObject(json);
myTeamsMessages=result.messages.ToList();
}
返回myTeamsMessages;
}
}
}
每次它针对任何beta端点运行时,都会抛出一个未经授权的错误,但针对v1.0端点,它工作正常


我遗漏了什么,我假设这与访问令牌有关,但我所做的所有研究都没有解决这个问题。

测试版仍可能更改,因此如果可能,请使用V1.0端点。对于未经授权的错误,我无法完全重现问题。根据我的测试,端点在Graph Explorer中运行良好:

https://graph.microsoft.com/beta(V1.0)/teams/{teamID}/channels/{channelid}/messages
但根据我的测试,端点在MVC/NETCore项目中不起作用,我使用了以下authtoken,但没有使用您使用的ADALToken:

string accessToken = await SampleAuthProvider.Instance.GetUserAccessTokenAsync(); 
我的总结异常是由令牌逻辑引起的,而不是HttpClient请求,我可以使用该请求处理其他api。另一种可能性,我们无法确认,API内部处理遇到了问题。但是我们可以在图形浏览器中使用API,所以这种可能性也不高。我想你可以试试其他代币,但不能先试试阿达尔肯牌