I have made a connection between an azure app and an online 365 excel document on my organisations sharepoint via Graph api. I was for a while able to create,update and delete rows in my table via the graph api and it all seemed to worked. But after a while i wasnt able to open my excel document in the browser and therefore is my document not updating anymore(the graph api is not working writing/updating). When i create a copy of the excel file and try to connect it again with my php code it works again for awhile and then stop working. I think it has something to do with the amount of session my code is making through graph api even though i only make one session and is closing that session again. Im using the same session id in my requests. Im not sure that its the problem, so that is why im asking here. I can still open the excel file in my desktopapp but its still not updating the rows.
My error when trying to open excel in browser is: “You do not have permission to open this file in Excel on the web, but you can open it in the desktop app.”
Before the Browser closes the document down for good, im experiencing this error for a while:
Error
My php code is this:
function create_session1 ($Token){
$getRowsUrl = "https://graph.microsoft.com/v1.0/users/{userid}/drive/items/{id}/workbook/createSession";
// Execute the request to get existing rows
$getRowsResponse = wp_remote_post($getRowsUrl, [
'headers' => [
'Authorization' => "Bearer $Token",
],
]);
// Check for errors in the get rows request
if (is_wp_error($getRowsResponse)) {
// Handle the error here
error_log("Error get: " . $getRowsResponse->get_error_message());
return false;
}
// Decode the JSON response
$getRowsResult = json_decode(wp_remote_retrieve_body($getRowsResponse), true);
return $sessionid = $getRowsResult['id'];
}
function close_session1($token, $sessionId) {
$closeSessionUrl = "https://graph.microsoft.com/v1.0/users/{userid}/drive/items/{id}/workbook/closeSession";
// Execute the request to close the session
$closeSessionResponse = wp_remote_request($closeSessionUrl, [
'method' => 'POST',
'headers' => [
'Authorization' => "Bearer $token",
'Workbook-Session-Id' => $sessionId,
],
]);
// Check for errors in the close session request
if (is_wp_error($closeSessionResponse)) {
// Handle the error here
error_log("Error closing session: " . $closeSessionResponse->get_error_message());
return false;
}
// Return true if the session was closed successfully
return true;
}
function get_clients_from_tb1($accessToken){
$args = array(
'headers' => array(
'Authorization'=> 'Bearer $Token',
),
);
// Modify the API URL to include the identifier
$apiUrl = 'https://system.easypractice.net/api/v1/clients?page_size=500';
$response = wp_remote_get($apiUrl, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
echo "Something went wrong1: $error_message";
return;
}
$status = wp_remote_retrieve_response_code($response);
if ($status !== 200) {
echo "Received HTTP status code: $status";
return;
}
$data = json_decode(wp_remote_retrieve_body($response), true);
$client_data = $data['data'][0]['phone'];
$all_client_ids_from_tb = array();
if (isset($data['data']) && is_array($data['data'])) {
// Loop through each element in the 'data' array
$sessionid = create_session1($accessToken);
foreach ($data['data'] as $client_data) {
// Do something with each element
$all_client_ids_from_tb[] = $client_data['id'];
post_data_to_excel1($accessToken,$client_data,$sessionid);
}
}
delete_rows_not_in_data1($accessToken,$all_client_ids_from_tb,$sessionid);
close_session1($accessToken,$sessionid);
}
get_clients_from_tb1($accessToken);
The code is executed by a cronjob on the server every hour.
I have tried to implement session via the graph API but it doesnt seem to have helped. It could be that is wrongly implemented. I have also tried the code on a fresh document.Furthermore i have tried to look for a solution to update an desktopapp version and forget about the browser version.