0
votes

Having issues getting Teams messages using the Office365 Graph.

I get the access token using

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

Then to get the Teams messages I use this as the endoint:

https://graph.microsoft.com/beta/teams/{teamID}/channels/{channelid}/messages

Using the access token recieve from above, and the endpoint I do the following:

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

Every time it runs against any of the beta endpoints, it throws an unauthorised error, but against the v1.0 endpoints it works fine.

What am i missing, im assuming its something to do with the access token but all the research ive done hasnt fixed the issue.

1

1 Answers

0
votes

Beta version is still subject to change, so if possible, please use the V1.0 endpoint. And for the unauthorised error, i cannot fully reproduce the problem. Based on my test, the endpoint works well in Graph Explorer:

https://graph.microsoft.com/beta(V1.0)/teams/{teamID}/channels/{channelid}/messages

But based on my test,the endpoint doesn't work in the MVC/NETCore project, I used the following authtoken but not the ADALToken which you use:

string accessToken = await SampleAuthProvider.Instance.GetUserAccessTokenAsync(); 

My summary the exception is caused by the token logic but not the HttpClient request, I can use the request to handle other api. Another possibility, we can't confirm that, the API internal processing has encountered a problem. But we can use the API in Graph Explorer, so this possibility is also not high. I think you can try other token but not the ADALToken first.