Using o365 MS Graph API to edit word document online. https://docs.microsoft.com/en-us/graph/api/driveitem-preview?view=graph-rest-beta This feature was working till 22nd JAN 2021. We are not able to edit document online from 27th JAN.
Code:
public static String getPreviewLinkWithEdit(String itemId, InlineEditorConfig config, boolean editFlag) throws JSONException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add(AUTHORIZATION, BEARER + config.getAccessToken());
String test = TEST;
if (!editFlag) {
test = TEST_WITHOUT_EDIT;
}
HttpEntity<String> request = new HttpEntity<>(test, headers);
ResponseEntity<String> response = restTemplate.exchange(HTTPS_GRAPH_BASE_URL_BETA + itemId + PREVIEW,
HttpMethod.POST, request, String.class);
return (null != response.getBody()
&& null != new JSONObject(response.getBody()).getString(GET_URL_STRING)
? new JSONObject(response.getBody()).getString(GET_URL_STRING)
: "");
}
Displaying the MS Word web URL in iframe
:
<iframe id="imagepgframe" src="https://purushothamchowdary-my.sharepoint.com/personal/purushotham_purushothamchowdary_onmicrosoft_com/_layouts/15/PreAuth.aspx?sourcedoc={289e7b7a-e5d3-43dc-957a-5564ca9faeb9}&action=edit#access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkcydDJKYzlkMVZ6RkdjdzZUZy02YUhZVXk2VSJ9%2EeyJhdWQiOiJ3b3BpL3B1cnVzaG90aGFtY2hvd2RhcnktbXkuc2hhcmVwb2ludC5jb21AZTZmMjQ4NWYtYTY4My00MzZlLThmZmEtMWQ3OWVkMjFmYWQwIiwiaXNzIjoiMDAwMDAwMDMtMDAwMC0wZmYxLWNlMDAtMDAwMDAwMDAwMDAwQDkwMTQwMTIyLTg1MTYtMTFlMS04ZWZmLTQ5MzA0OTI0MDE5YiIsIm5iZiI6IjE2MTE5MDM0NDQiLCJleHAiOiIxNjExOTAzNzQ0IiwibmFtZWlkIjoiMTAwMzIwMDEwYTNiNGM0MyIsIm5paSI6InVybjpmZWRlcmF0aW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UzBty%2DNP2WUtDibf%2DKzOCTTW9aF01ytp%5FQTqIikpdDLUkyXQwDuJQzKuZh8vjxSXF5jfYtoK7JyqUNXZfllozebrqXe80arK%2DTNvvqAk3tOqF5yBedEYW0KrIIyFlyPNXNMyxdGBNEvRjMpMBqzE0Pn3wBiWeh6q4Bx8qSCYE9HwObPDUNtw%2DtCZh%2DFx6suS%5F3kuntWjyxKOqYRfYWpt%5FrWwFCxbFvWwThG5JcT1RUWHi0vdvA9%2DVGn%5FMUqxuKr9BQjUaQC9SFIhaMw"></iframe>
The word document file is getting uploaded to OneDrive. In application getting the web URL to open the word document in browser. The application java code works fine without giving any error .
Preview Link :
https://graph.microsoft.com/beta/me/drive/items/01ZPAXLBD2POPCRU7F3RBZK6SVMTFJ7LVZ/preview
In iframe
bellow error getting displayed:
Error in browser console:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://word-edit.officeapps.live.com') does not match the recipient window's origin ('https://sheethaljav-my.sharepoint.com').
Cookie:
question from:
https://stackoverflow.com/questions/65932653/unable-to-edit-word-document-online-using-o365-ms-graph-api-getpreviewlinkwithed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…