通过仅应用程序主体的Sharepoint REST API突然停止工作

问题描述

我们正在使用Sharepoint Rest API索引内容

由于客户的安全要求,我们只能将在Sharepoint管理中心中授予的API权限与仅包含秘密的应用程序主体结合使用。

我们在Azure AD Admin Center中注册一个应用程序,并按here所述授予了该权限。

然后,我们编写了一段Java代码,以适应here中所述的请求以检索JWT:

List<NameValuePair> params = new ArrayList<>(10);
        params.add(new BasicNameValuePair("grant_type","client_credentials"));
        params.add(new BasicNameValuePair("client_id",clientId + "@" + directoryId));
        params.add(new BasicNameValuePair("client_secret",clientSecret));
        params.add(new BasicNameValuePair("resource","00000003-0000-0ff1-ce00-000000000000/" + targetHost + "@" + directoryId));

        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
            HttpPost post = new HttpPost("https://accounts.accesscontrol.windows.net/" + directoryId + "/tokens/OAuth/2");
            post.setEntity(new UrlEncodedFormEntity(params,Consts.UTF_8));
            String result;
            try (CloseableHttpResponse response = httpClient.execute(post)) {
                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
                    throw new SharepointClient.HttpStatusException(response.getStatusLine().getStatusCode());
                result = Strings.fromUtf8(IO.readAll(response.getEntity().getContent()));
            }
            accesstoken = new JsonData(result).get("access_token");
        }

然后,在对API的所有请求的授权标头中,将该令牌用作承载令牌。

直到大约一周前,这种方法在几个月内都工作良好。从那以后,我们只收到403条回复

HttpResponseProxy{HTTP/1.1 403 Forbidden [Cache-Control: private,max-age=0,transfer-encoding: chunked,Content-Type: application/json;odata=verbose;charset=utf-8,Expires: Wed,30 Sep 2020 09:58:49 GMT,Last-Modified: Thu,15 Oct 2020 09:58:49 GMT,P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI",X-SharePointHealthscore: 2,X-MSDAVEXT_Error: 917656; Zugriff+verweigert.+Zum+%c3%96ffnen+von+Dateien+an+diesem+Speicherort+m%c3%bcssen+Sie+zun%c3%a4chst+zur+Website+wechseln+und+die+Option+zur+automatischen+Anmeldung+aktivieren.,DATASERVICEVERSION: 3.0,X-AspNet-Version: 4.0.30319,SPRequestGuid: f7dd839f-10d1-2000-73fa-40c47dd29bbb,request-id: f7dd839f-10d1-2000-73fa-40c47dd29bbb,MS-CV: n4Pd99EQACBz+kDEfdKbuw.0,Strict-Transport-Security: max-age=31536000,x-frame-options: SAMEORIGIN,Content-Security-Policy: frame-ancestors 'self' teams.microsoft.com *.teams.microsoft.com *.skype.com *.teams.microsoft.us local.teams.office.com *.powerapps.com *.yammer.com *.officeapps.live.com *.stream.azure-test.net *.microsoftstream.com;,X-Powered-By: ASP.NET,MicrosoftSharePointTeamServices: 16.0.0.20530,X-Content-Type-Options: nosniff,X-MS-InvokeApp: 1; RequireReadOnly,X-MSEdge-Ref: Ref A: 330CB0BA15E647CC8B732FCC45D4F950 Ref B: AM3EDGE0620 Ref C: 2020-10-15T09:58:49Z,Date: Thu,15 Oct 2020 09:58:49 GMT] ResponseEntityProxy{[Content-Type: application/json;odata=verbose;charset=utf-8,Chunked: true]}}

错误消息是德语(我不知道为什么,整个系统设置为英语)。它说:“访问被拒绝。要在此位置打开文件,您必须先进入网站并激活自动登录选项。”

有人知道可能会发生什么变化吗?我们需要更改请求中的某些内容吗?

我们已经检查了:

  • 秘密没有过期
  • 通过证书和Azure AD管理中心权限的身份验证仍在起作用(相同的API请求,只是令牌检索是使用msal4j完成的)
  • 通过Postman发送令牌请求和API请求会得到相同的结果

解决方法

发生此错误的原因不是您没有权限。根据错误消息Access denied. To open files in this location,you have to first go to the website and activate the option for automatic login

您需要:

1。对Office 365进行身份验证。

2。将SharePoint Online网站添加到受信任的网站。

3。检查WebClient服务的状态。

请参阅:Troubleshoot mapped network drives that connect to SharePoint Online