![](/img/trans.png)
[英]Unauthorized: Access is denied due to invalid credentials . Microsoft Graph API for sharepoint
[英]Graph API for C#: 401 - Unauthorized: Access is denied due to invalid credentials. on /planner/plans
我正在使用 Graph API。 我沒有太多這方面的經驗。 我正在創建一個應用程序以將計划添加到現有組中。
所以,我已經注冊了一個具有此權限的應用程序(您可以看到這些是委托權限):
然后,我編寫了這段代碼來使用這個端點 /planner/plans 添加一個新的 Planner(你會看到我正在使用一個令牌)。
var configAuthority = "https://login.microsoftonline.com/8318d89e-e4db-471e-85da-b34ae833813f";
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(ConfigurationParameters.ClientID)
.WithTenantId(ConfigurationParameters.TenantID)
.WithClientSecret(ConfigurationParameters.ClientSecret)
.WithAuthority(new Uri(configAuthority))
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
AuthenticationResult accessToken = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync().Result;
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
List<HeaderOption> requestHeaders = new List<HeaderOption>() {
new HeaderOption("Authorization", "Bearer " + accessToken.AccessToken)
};
var existingsplanners = graphClient
.Groups[createPlannerForGroupRequest.GroupId.ToString()]
.Planner
.Plans.
Request(requestHeaders)
.GetAsync()
.Result;
var planner = graphClient
.Groups[createPlannerForGroupRequest.GroupId.ToString()]
.Planner
.Plans
.Request(requestHeaders)
.AddAsync(new PlannerPlan {
Title = createPlannerForGroupRequest.Name,
Owner = createPlannerForGroupRequest.GroupId.ToString(),
ODataType = null,
}).Result;
兩個請求的結果是相同的。
Code: UnknownError
Message: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
<h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
</fieldset></div>
</div>
</body>
</html>
Inner error:
AdditionalData:
request-id: 19c09b10-1694-4201-85a9-ba3639e1dba5
date: 2020-06-03T07:29:59
ClientRequestId: 19c09b10-1694-4201-85a9-ba3639e1dba5
如您所見,即使它是 Azure AD 上的委派權限,也存在權限問題。 我應該等24小時嗎? 我應該更改令牌請求者方法而不是使用 AcquireTokenForClient 嗎?
相同的方法適用於 Graph Explorer。 為現有組創建 Team 實例可以正常工作。
謝謝!
似乎您在代碼中使用了客戶端憑據授予流程,但圖表 api 僅支持委派權限。 所以我們需要使用授權碼授權流程或用戶名/密碼授權流程。 (順便說一下,我們推薦授權碼流而不是用戶名/密碼流)
有關授權代碼流程,請參閱本教程,其中包含代碼示例。
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithRedirectUri(redirectUri)
.WithClientSecret(clientSecret) // or .WithCertificate(certificate)
.Build();
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);
對於用戶名/密碼流程,您可以參考本教程以及其中的代碼示例。
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.Build();
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
User me = await graphClient.Me.Request()
.WithUsernamePassword(email, password)
.GetAsync();
更新:
您可以按照以下步驟進行一些更改,然后您不需要在代碼中提供 client_secret 並且它也不會顯示錯誤消息The request body must contain the following parameter: 'client_assertion' or 'client_secret'
。
我已嘗試使用證書創建身份驗證。 首先,在應用程序注冊中應用該配置(僅具有應用程序權限)並從您之前共享的鏈接中閱讀一些指南后,我從 /Groups 獲得並且它有效。 之后,向 /Groups/{XXX-XX}/Planner/Plans 創建了一個新請求並收到權限錯誤。 創建證書的方法來自這個鏈接: https://laurakokkarinen.com/authenticating-to-office-365-apis-with-a-certificate-step-by-step/這是獲取所有組的代碼。
var url = "https://graph.microsoft.com";
var auth = new Authorization(ConfigurationParameters.TenantID,
ConfigurationParameters.ClientID,
"34 64 03 a4 9c ad xx xx xx xx xx xx xx xx xx xx xx xx xx xx",
@"C:\temp\Certificate.pfx",
"Come up with something secure!");
var accessToken = auth.GetAccessTokenAsync(url).Result;
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
string urlReq = "https://graph.microsoft.com/v1.0/groups/" + createPlannerForGroupRequest.GroupId.ToString() + "";
var response = httpClient.GetAsync(urlReq);
Stream receiveStream = response.Result.Content.ReadAsStreamAsync().Result;
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var r = readStream.ReadToEnd();
return r;
}
那行得通,我可以閱讀要求的組,但是當我應用此端點時:
string urlReq = "https://graph.microsoft.com/v1.0/groups/" + createPlannerForGroupRequest.GroupId.ToString() + "/planner/plans";
我收到此錯誤消息:
{
"error": {
"code": "UnknownError",
"message": "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>\r\n<title>403 - Forbidden: Access is denied.</title>\r\n<style type=\"text/css\">\r\n<!--\r\nbody{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}\r\nfieldset{padding:0 15px 10px 15px;} \r\nh1{font-size:2.4em;margin:0;color:#FFF;}\r\nh2{font-size:1.7em;margin:0;color:#CC0000;} \r\nh3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} \r\n#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:\"trebuchet MS\", Verdana, sans-serif;color:#FFF;\r\nbackground-color:#555555;}\r\n#content{margin:0 0 0 2%;position:relative;}\r\n.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n-->\r\n</style>\r\n</head>\r\n<body>\r\n<div id=\"header\"><h1>Server Error</h1></div>\r\n<div id=\"content\">\r\n <div class=\"content-container\"><fieldset>\r\n <h2>403 - Forbidden: Access is denied.</h2>\r\n <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>\r\n </fieldset></div>\r\n</div>\r\n</body>\r\n</html>\r\n",
"innerError": {
"request-id": "3ddaf510-6c16-403e-a365-4f2c6f27032c",
"date": "2020-06-05T06:08:24"
}
}
}
我想我這次明白了:),但我得到了一個錯誤。 我現在使用這種方式來獲取令牌:
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(ConfigurationParameters.ClientID)
.WithTenantId(ConfigurationParameters.TenantID)
.Build();
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
var token = authProvider.ClientApplication.AcquireTokenByUsernamePassword(scopes,u, ToSecureString(p));
var t = token.ExecuteAsync().Result;
沒有證書,只有用戶名和密碼。 我收到此錯誤消息:
{"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: d75e8aea-4d4e-4d34-a015-efd1d7f53600\r\nCorrelation ID: c3a5e38f-91e3-488c-91de-e850eef19219\r\nTimestamp: 2020-06-05 07:20:38Z","error_codes":[7000218],"timestamp":"2020-06-05 07:20:38Z","trace_id":"d75e8aea-4d4e-4d34-a015-efd1d7f53600","correlation_id":"c3a5e38f-91e3-488c-91de-e850eef19219","error_uri":"https://login.microsoftonline.com/error?code=7000218"
API 中的權限被委派給 Group.ReadAll 和 WriteAll。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.