【问题标题】:HowTrying to consume Rest api of Sharepoint from java client but getting status code 403 Forbidden如何尝试从 Java 客户端使用 Sharepoint 的 Rest api 但获取状态码 403 Forbidden
【发布时间】:2015-04-14 00:50:24
【问题描述】:

我正在尝试通过 java 客户端使用 Sharepoint 的 rest api 获取文件,但得到 403 Forbidden 错误代码。

    Client c = Client.create();
    WebResource resource = c.resource("http://URL/_api/web/GetFolderByServerRelativeUrl('/Folder')/Files");     
    String userCredentials = "Username:Password";
    resource.header("Authorization", "Basic " + new String(new Base64().encode(userCredentials.getBytes())));
    resource.header("Accept","application/json; odata=verbose");
    String response = resource.get(String.class);

我在标题中发送授权仍然面临同样的错误。也用soap wsdl尝试了同样的事情,但得到了同样的回应。

【问题讨论】:

    标签: java rest sharepoint soap


    【解决方案1】:

    这就是我使用 NTML 进行身份验证的方式 - 技巧是从这个示例更改为 NTCredentials() 与 UsernamePasswordCredentials() -> https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientAuthentication.java

    Maven 依赖: org.apache.http 组件 httpclient 4.4.1

    public class SharePointClientAuthentication {
    
    public static void main(String[] args) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY),
            new NTCredentials("username", "password", "https://hostname", "domain"));
        CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
        try {
            HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");
    
            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
           } finally {
            response.close();
        }
        } finally {
            httpclient.close();
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2015-11-28
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多