簡體   English   中英

Office 365圖獲得Microsoft Teams消息

[英]Office 365 Graph getting Microsoft Teams Messages

使用Office365圖獲取Teams消息時遇到問題。

我使用獲取訪問令牌

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

然后,要獲取“團隊”消息,我將其用作Endint:

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

使用上面的訪問令牌和端點,我執行以下操作:

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

每次針對任何Beta端點運行時,都會引發未授權的錯誤,但是針對v1.0端點,則可以正常工作。

我想念的是什么,我以為它與訪問令牌有關,但是所有已完成的研究尚未解決問題。

Beta版本仍可能更改,因此,請盡可能使用V1.0端點。 對於未經授權的錯誤,我無法完全重現該問題。 根據我的測試,端點在Graph Explorer中運行良好:

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

但是根據我的測試,端點在MVC / NETCore項目中不起作用,我使用了以下身份驗證令牌,但沒有使用您使用的ADALToken:

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

我的摘要中,異常是由令牌邏輯引起的,而不是由HttpClient請求引起的,我可以使用該請求來處理其他api。 另一種可能性,我們無法確認,API內部處理遇到了問題。 但是我們可以在Graph Explorer中使用API​​,因此這種可能性也不高。 我認為您可以先嘗試其他令牌,但不能嘗試ADALToken。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM