Udesh Sendanayake
Udesh Sendanayake

Reputation: 107

Read Excel file in Sharepoint using java

I have an excel file in Sharepoint with read permissions and I need to read/download it using java. Below code example gives 403 Forbidden error. Please help me to resolve this.

 CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(AuthScope.ANY),
                new NTCredentials("username", "password", "https://abc-my.sharepoint.com", "abc"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        try {
            HttpGet httpget = new HttpGet("https://abc-my.sharepoint.com/:x:/r/personal/_layouts/15/Doc.aspx?sourcedoc=%dedededed-9889-098-AAAA-wbxcdssj%7D&file=my_excel.xlsx");


            CloseableHttpResponse response = httpclient.execute(httpget);
            try {

                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }

Upvotes: 4

Views: 6653

Answers (1)

Amos
Amos

Reputation: 2091

You can refer to the following blog to authenticate requests in java code:Java-service integration with SharePoint Online via REST API

Or you could read excel file with Microsoft Graph API and granting access using SharePoint App-Only.

Microsoft Graph Auth Preview SDK for Java
HOW READ METADATA IN EXCEL SHAREPOINT FILE (JSON)

Upvotes: 3

Related Questions