[英]SharePoint App-Only Authentication throwing “The remote server returned an error: (401) Unauthorized.”
I am trying to make sharepoint authentication using oAuth or App-Only authentication, but it gives error "The remote server returned an error: (401) Unauthorized."我正在尝试使用 oAuth 或 App-Only 身份验证进行 sharepoint 身份验证,但它给出了错误“远程服务器返回错误:(401)未经授权。”
I am using trial sharepoint tenant.我正在使用试用版 sharepoint 租户。
Here is my code snippets:这是我的代码片段:
Using Client id & Client secret:使用客户端 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);
};
Using AccessToken:使用访问令牌:
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
I referred below articles: 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/我参考了以下文章: 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/
This could be SharePoint app-only permissions is disabled in your tenant.这可能是 SharePoint 在您的租户中禁用了仅应用权限。
You could run the command to enabel it: Set-SPOTenant -DisableCustomAppAuthentication $false
您可以运行命令来启用它: Set-SPOTenant -DisableCustomAppAuthentication $false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.