簡體   English   中英

SharePoint 僅應用程序身份驗證拋出“遠程服務器返回錯誤:(401)未經授權。”

[英]SharePoint App-Only Authentication throwing “The remote server returned an error: (401) Unauthorized.”

我正在嘗試使用 oAuth 或 App-Only 身份驗證進行 sharepoint 身份驗證,但它給出了錯誤“遠程服務器返回錯誤:(401)未經授權。”

我正在使用試用版 sharepoint 租戶。

這是我的代碼片段:

使用客戶端 ID 和客戶端密碼:

string siteUrl = "[Sharepoint-Site-URL]";

//Sharepoint App details
string SPClientId = @"[Client-id]";
string SPClientSecret = @"[Client-secret]";
using (ClientContext context = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, SPClientId, SPClientSecret))
 {
     context.Load(context.Web, t => t.Title);
     context.ExecuteQuery();
     Console.WriteLine(context.Web.Title);
 };

使用訪問令牌:

 string siteUrl = "[Sharepoint-Site-URL]";
 string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(siteUrl));

 //Get the access token for the URL.  
 string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(siteUrl).Authority, realm).AccessToken;

 //Create a client context object based on the retrieved access token
 using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken))
 {
    context.Load(cc.Web, t => t.Title);
    context.ExecuteQuery();
    Console.WriteLine(cc.Web.Title);
 }
enter code here

我參考了以下文章: https: //docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs https://www.c-sharpcorner.com/article/connect-to- sharepoint-online-site-with-app-only-authentication/

這可能是 SharePoint 在您的租戶中禁用了僅應用權限。

您可以運行命令來啟用它: Set-SPOTenant -DisableCustomAppAuthentication $false

暫無
暫無

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

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