首页 文章

从守护程序应用程序访问租户Microsoft Graph

提问于
浏览
1

我正在开发一个守护程序应用程序,它与Microsoft 365 Office Planner交互以操作Microsoft Graph任务 . 当我调用Microsoft Graph API来获取与我的租户相关的任务时,我收到了一个未经授权的请求异常 .

我已在Azure Active Directory中注册了我的应用程序,并授予其使用Microsoft Graph的权限 .

我从这里请求基于进程的访问令牌:https://graph.microsoft.io/en-us/docs/authorization/app_only

我能够从Azure Active Directory v2.0 endpoints 获取令牌 . 请求代码如下:

new KeyValuePair<string, string>("grant_type", "client_credentials"),
         new KeyValuePair<string, string>("client_id", "<clent id>"),
         new KeyValuePair<string, string>("client_secret", "<client secret>"),
         new KeyValuePair<string, string>("resource", @"https://graph.microsoft.com")
     var content = new FormUrlEncodedContent(pairs);

     var response = client.PostAsync("https://login.microsoftonline.com/<tenant id>/oauth2/token", content).Result;

当我使用此访问令牌执行如下请求时:

client.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Bearer", token);

     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

     var response = client.GetAsync(@"https://graph.microsoft.com/beta/tasks").Result;

我收到状态代码401 Unauthorized,其中包含以下响应消息:

Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.

是否有任何授权过程我没有执行授权访问我的应用程序 . 请帮忙!!!!

提前致谢!!

1 回答

  • 0

    根据测试,似乎Microsoft Graph不支持使用仅限app令牌列出任务 . 在我向应用程序授予 Group.ReadAll app权限后,我收到了如下错误请求:

    GET:https://graph.microsoft.com/beta/tasks?$filter=createdBy+eq+'xx@xxxx.onmicrosoft.com'
    

    enter image description here

    但是,使用具有相同权限的委托令牌成功调用了请求:
    enter image description here

    作为解决方法,您可以检查 OAuth2 Code Grant flow 是否对您的方案有用 .

    其他用户已使用仅限应用令牌提出了有关列出任务的反馈,如果您还需要此功能,则可以从here投票反馈 .

相关问题