I want to manupulate (read, write, delete) files in a document library of my sharepoint (hosted at Sharepoint Online). Ultimately this will be Part of a Java Webapplication where a backend job runs regulary, generates Files and uploads them to Sharepoint. For now I'm working in a JUnit Test to get familiar with Sharepoints REST API.
And this is where trouble comes in.
What I tried so far…
1. SPOnline Library
- Github: https://github.com/quantr-research/Sharepoint-java-api
- Tutorial: https://www.youtube.com/watch?v=j4QzjJDepLU
I found this little library and tried it
// This connects to https://login.microsoftonline.com/extSTS.srf
// Token contains Cookies rtFa and FedAuth that will be added to all future requests
// Username and password are those credentials I would use to login at mysharepoint.sharepoint.com
String domain = "mysharepoint";
Pair<String, String> token = SPOnline.login(USERNAME, PASSWORD, domain);
// Connects to mysharepoint.sharepoint.com/_api/contextinfo
// Returns a form digest value that looks okay: 0xBIGLONGHEXADECIMALSTRING,27 May 2020 08:58:09 -0000
String jsonString = SPOnline.post(token, domain, "_api/contextinfo", null, null);
JSONObject json = new JSONObject(jsonString);
String formDigestValue = json.getJSONObject("d").getJSONObject("GetContextWebInformation").getString("FormDigestValue");
String path = "/sites/MySite/_api/web/GetFolderByServerRelativeUrl('/MySite/Test-Doc-Lib/Test-Folder/Test-File.xlsx')";
String deleteResponseJson = SPOnline.delete(token, domain, path, formDigestValue);
The Delete Request returns a 403 error. The response JSON says
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
2. OAuth and Bearer Token, App Registration in Sharepoint
(To be honest I'm not sure if SPOnline doesn't use OAuth as well but at least there is no Bearer Token involved there)
- https://docs.microsoft.com/de-de/sharepoint/dev/sp-add-ins/register-sharepoint-add-ins
- http://www.ktskumar.com/2017/01/access-sharepoint-online-using-postman/
According to the second link I do the following:
- I go to
https://mysharepoint.sharepoint.com/sites/MySite/_layouts/15/appregnew.aspx
generate Client-ID and Client Secret and setTitle=Test-SP-App
,App-Domain=localhost
,Redirect-URI=https://localhost
. - Next I go to
https://softwareschmiederaeder.sharepoint.com/sites/MySite/_layouts/15/appinv.aspx
and grant permissions. - With a call to
https://mysharepoint/sharepoint.com/_vti_bin/client.svc/
I get the tenant ID - With a call to
https://accounts.accesscontrol.windows.net/<TenantID>/tokens/OAuth/2
I get a Bearer Access token
When Accessing any Sharepoint Ressource with this token I get a 403 error
{
"error": {
"code": "-2147024891, System.UnauthorizedAccessException",
"message": {
"lang": "de-DE",
// Translation: Access denied. You don't have permission to execute this process or access this ressource.
"value": "Zugriff verweigert. Sie haben keine Berechtigung, diesen Vorgang auszuführen oder auf diese Ressource zuzugreifen."
}
}
}
3. App registration in Azure
This post is about registering... something in Azure and then granting permissions to Sharepoint. I get to the point where I get a new access token in postman. A windows opens and asks for my credentials (which is not even acceptable, as I need to run this in a Java backend job where no user is present). When I enter my cretendials the login fails.
4. JShare
I also tried JShare
which is not free but has 30 day trial, hoping to do a proof of concept.
Service service = new Service("https://mysharepoint.sharepoint.com", USERNAME, PASSWORD);
Folder folder = service.createFolder("/TestDocLib/CreatedFolder");
Again I get a 403 error.
5. JCIFS
I followed this post... again 403 error.
Solution Ideas
I'm a littly overwhelmed with these many possibilities. Right now I could imagine the following:
- The account I use (USERNAME, PASSWORD) to login with Sharepoint dosn't have correct permissions. Since I'm the owner of the site MySite I somehow doubt it. I even tried giving me explicit permission by right click on
Test-Folder
andManage Access
. - I send my requests to the wrong urls. In the tutorial with my first attempt (SPOnline) the urls only contain
/mysite/...
while browsing my sharepoint always shows the prefix "site":/site/mysite
. However if I remove that prefix I get a 404 error so I there is not even something to authenticate to.