C# 从图形资源管理器工作时,无法从c控制台应用程序获取团队详细信息
我需要使用MicrosoftGraph从C控制台应用程序收集MicrosoftTeams数据 我正在使用ADAL并从样本中克隆身份验证方法。 唯一的区别是我使用的是HttpClient客户端,而不是不实现Teams对象的GraphServiceClient。 所需权限的列表已通过Fiddler跟踪由Graph Explorer发出的不需要User.Read.All或User.Write.All的请求来确定: User.Read、Mail.Send、Files.ReadWrite、User.ReadWrite、User.ReadBasic.All、Sites.ReadWrite.All、Contacts.ReadWrite、People.Read、Notes.ReadWrite.All、Tasks.ReadWrite、Mail.ReadWrite、Files.ReadWrite.All、Calendars.ReadWrite 只要我不请求任何团队资源,我的控制台应用程序一切正常: 我可以得到以下团队的团队列表 请求:https://graph.microsoft.com/beta/groups?$filter=resourceProvisioningOptions/anyv:v eq'Team'&$select=id、displayname、groupTypes、resourcebhavioroptions、resourceProvisioningOptions 我可以通过以下方式成功获取组详细信息:https://graph.microsoft.com/beta/groups/{groupId} 但是,当我尝试获取我所属组的团队视图时,HTTP失败了 403未经授权: https://graph.microsoft.com/beta/groups/{groupId}/团队 非常 令人沮丧的是,看到这最后一步从 图形浏览器 我的问题与非常相似,但在我的情况下,我是我试图访问的团队的成员,请求与Graph Explorer一起工作 代码详细信息:C# 从图形资源管理器工作时,无法从c控制台应用程序获取团队详细信息,c#,microsoft-graph-api,adal,C#,Microsoft Graph Api,Adal,我需要使用MicrosoftGraph从C控制台应用程序收集MicrosoftTeams数据 我正在使用ADAL并从样本中克隆身份验证方法。 唯一的区别是我使用的是HttpClient客户端,而不是不实现Teams对象的GraphServiceClient。 所需权限的列表已通过Fiddler跟踪由Graph Explorer发出的不需要User.Read.All或User.Write.All的请求来确定: User.Read、Mail.Send、Files.ReadWrite、User.Rea
你错过了一个范围。你需要有Group.Read.All才能阅读。我不这么认为,因为它适用于没有它的组。此外,对于团队详细信息的请求正在使用图形资源管理器而不使用它。默认情况下,图形资源管理器包括Group.ReadWrite.All。这是Group.Read.All的超集,因此图形浏览器使用此范围。在调用/team endpoint之前,您需要拥有这两个作用域中的一个。您肯定是对的:添加Group.Read.All作用域修复了问题。我甚至可以请求/beta/me/JoinedTeams。。。它不适用于图形资源管理器。有一件事我仍然不明白:除非我错过了它,否则为什么当嗅探来自Graph Explorer的流量时,Fiddler跟踪中缺少Group.Read.All作用域?
class AuthenticationHelper
{
// The Client ID is used by the application to uniquely identify itself to the v2.0 authentication endpoint.
static string clientId = Constants.ClientId;
// The list of required permissions have been determined with a Fiddler trace of a request made with Graph Explorer
// e.g. below are the permissions Grap Explorer requires to run the sample requests
public static string[] Scopes = {
"User.Read"
, "Mail.Send"
, "Files.ReadWrite"
, "User.ReadWrite"
, "User.ReadBasic.All"
, "Sites.ReadWrite.All"
, "Contacts.ReadWrite"
, "People.Read"
, "Notes.ReadWrite.All"
, "Tasks.ReadWrite"
, "Mail.ReadWrite"
, "Files.ReadWrite.All"
, "Calendars.ReadWrite"
};
public static PublicClientApplication IdentityClientApp = new PublicClientApplication(clientId);
public static string UserToken = null;
public static DateTimeOffset Expiration;
//-----------------------------------------------------------------------------------------------------------------
public static async Task<HttpClient> GetAuthenticatedHttpClient()
{
HttpClient client = null;
try
{
client= new HttpClient(new HttpClientHandler { UseCookies = true });
var token = await GetTokenForUserAsync();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
client.DefaultRequestHeaders.Add("SampleID", "TestCSharp-AzureToken");
return client;
}
catch (Exception ex)
{
Debug.WriteLine("Could not create a graph client: " + ex.Message);
}
return client;
}
//-----------------------------------------------------------------------------------------------------------------
public static async Task<string> GetTokenForUserAsync()
{
AuthenticationResult authResult;
try
{
IEnumerable<IAccount> accounts = await IdentityClientApp.GetAccountsAsync();
IAccount firstAccount = accounts.FirstOrDefault();
authResult = await IdentityClientApp.AcquireTokenSilentAsync(Scopes, firstAccount);
UserToken = authResult.AccessToken;
}
catch (Exception)
{
if (UserToken == null || Expiration <= DateTimeOffset.UtcNow.AddMinutes(5))
{
authResult = await IdentityClientApp.AcquireTokenAsync(Scopes );
UserToken = authResult.AccessToken;
Expiration = authResult.ExpiresOn;
}
}
return UserToken;
}
}
//----------------------------------------------------
// Console entry point
class Program
{
//public static GraphServiceClient client;
public static HttpClient _client;
static async Task<string> GetHttpResponse(string url)
{
string responseBody = null;
_client = await AuthenticationHelper.GetAuthenticatedHttpClient();
HttpResponseMessage response = await _client.GetAsync(url);
response.EnsureSuccessStatusCode();
using (HttpContent content = response.Content)
{
responseBody = await response.Content.ReadAsStringAsync();
logger.Trace(responseBody);
}
return responseBody;
}
static void Main(string[] args)
{
// call 1 is working: list groups that "are Microsoft Teams"
string s;
string url = "https://graph.microsoft.com/beta/groups?$filter=resourceProvisioningOptions/any(v:v eq 'Team')&$select=id,displayname,groupTypes,resourceBehaviorOptions,resourceProvisioningOptions";
s = await GetHttpResponse(url);
Console.WriteLine(s);
// call 2 is working: Display details of one of these groups
Console.Write($"Enter the id of the group/teams to search for: ");
string groupId = Console.ReadLine().Trim().ToLower();
url = $"https://graph.microsoft.com/beta/groups/{groupId}";
s = await GetHttpResponse(url);
Console.WriteLine(s);
// call 3 is failing: Display the team view of this groups
url = url + "/team";
s = await GetHttpResponse(url);
Console.WriteLine(s);
}
}