package com.tgt.ding.sp.reader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
public class SharePointDownload {
private static final String SHAREPOINT_SITE = "https://basanonline.sharepoint.com/sites/ogrp-ds-basan";
private static final String DOCUMENT_LIBRARY = "Documents";
private static final String FILE_NAME = "Book.xlsx";
private static final String CLIENT_ID = "your-client-id";
private static final String CLIENT_SECRET = "your-client-secret";
private static final String ACCESS_TOKEN = getAccessToken(CLIENT_ID, CLIENT_SECRET); // Implement access token retrieval logic
public static void main(String[] args) throws URISyntaxException, IOException {
String downloadUrl = buildDownloadUrl(SHAREPOINT_SITE, DOCUMENT_LIBRARY, FILE_NAME);
downloadFile(downloadUrl, ACCESS_TOKEN);
}
private static String buildDownloadUrl(String siteUrl, String documentLibrary, String fileName) {
return siteUrl + "/_api/web/GetFileByServerRelativePath(decodedurl='/sites/ogrp-ds-basan/Shared%20Documents/DataFolder/testCrawl/Book.xlsx')/$value";
}
private static void downloadFile(String downloadUrl, String accessToken) throws URISyntaxException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet(new URI(downloadUrl));
request.addHeader("Authorization", "Bearer " + accessToken);
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
InputStream inputStream = response.getEntity().getContent();
FileOutputStream outputStream = new FileOutputStream("Book.xlsx");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
System.out.println("File downloaded successfully!");
} else {
System.out.println("Error downloading file: " + statusCode);
}
}
}
// Replace this with your access token retrieval logic using Azure AD
private static String getAccessToken(String clientId, String clientSecret) {
// Implement logic to get access token using Azure AD and OAuth 2.0 flow
return "token";
}
}
Get tenantid and resourceid
curl --request GET \
--url https://basanonline.sharepoint.com/sites/ogrp-ds-basan/_vti_bin/client.svc \
--header 'Authorization: Bearer bearer' \
--header 'User-Agent: insomnia/8.6.1'
Look into www-authenticate header in response
|
Getting token using clientid, clientsecret and tenantid and clientidresource
curl --request POST \
--url https://accounts.accesscontrol.windows.net/tenantid/tokens/OAuth/2 \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data client_id=clientid@tenantid\
--data 'client_secret=ClientSECRET' \
--data resource= clientidresource/basanonline.sharepoint.com@ tenantid
https://sharepointcass.com/2021/04/01/sharepoint-online-rest-apis-part-iii-pages/