使用 C# SDK 从 Beta 图形 API 调用获取响应标头



我使用 Beta Graph API 创建团队频道。

根据测试版文档,这将返回一个指示团队 ID 的202和标头

HTTP/1.1 202 Accepted
Content-Type: application/json
Location: /teams/{teamId}/operations/{operationId}
Content-Location: /teams/{teamId}
Content-Length: 0

我的代码

var team = new Team
{
DisplayName = className,
Description = description,
AdditionalData = new Dictionary<string, object>()
{
{"template@odata.bind", "https://graph.microsoft.com/beta/teamsTemplates('educationClass')"},
},
Owners = new TeamOwnersCollectionWithReferencesPage() { new User() { Id = ownerId } }
};

await graphClient.Teams
.Request()
.AddAsync(team, cancellationToken);

AddAsync返回的响应似乎始终为 null,并且似乎没有办法通过 SDK 访问标头。我现在必须跳过冗长的箍才能获得 ID。

有什么办法可以得到这个吗?

SDK 现在具有以下方法

AddResponseAsync

它返回:

Task<GraphResponse<Team>> 

所以我使用以下代码来提取团队 ID

string pattern = @"([a-z0-9]{8}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{12})";
var str = response.Content.Headers.ContentLocation.OriginalString;
var match = Regex.Match(str, pattern);
var newteamId = match.Value;
return newteamId;

您可以获取已保存团队的 id 值,如下所示。

var x = response.Content.Headers.GetValues("Content-Location");

你会得到这样的/teams/{teamId} 格式。

最新更新