C# 仅SharePoint应用程序身份验证抛出“;远程服务器返回错误:(401)未经授权。”;
我正在尝试使用oAuth或仅应用程序身份验证进行sharepoint身份验证,但它给出错误“远程服务器返回错误:(401)未经授权。” 我正在使用试用sharepoint租户 以下是我的代码片段: 使用客户端id和客户端密码:C# 仅SharePoint应用程序身份验证抛出“;远程服务器返回错误:(401)未经授权。”;,c#,oauth,sharepoint-online,C#,Oauth,Sharepoint Online,我正在尝试使用oAuth或仅应用程序身份验证进行sharepoint身份验证,但它给出错误“远程服务器返回错误:(401)未经授权。” 我正在使用试用sharepoint租户 以下是我的代码片段: 使用客户端id和客户端密码: string siteUrl = "[Sharepoint-Site-URL]"; //Sharepoint App details string SPClientId = @"[Client-id]"; string SPCli
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
使用AccessToken:
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
我参考了以下文章:
这可能是因为您的租户中禁用了SharePoint应用程序权限
您可以运行命令来启用它:
Set SPOTenant-DisableCustomAppAuthentication$false
sharepoint是azure资源吗?我今天也收到了401错误太好了,非常感谢@Michael Han!