Hello,
I'm Nick. Our team built a WordPress plugin written in PHP that integrates OneDrive as a remote storage solution.
Recently, some users have reported issues downloading backups from OneDrive.
To connect OneDrive, users need to authenticate their accounts. Here are the authorization codes used in the process. Once authentication is complete, the plugin retrieves the following information: access_token, refresh_token, and expires_in.
$auth_id = uniqid('wpvivid-auth-');
$url = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
. '?client_id=' . urlencode('37668be9-b55f-458f-b6a3-97e6f8aa10c9')
. '&scope=' . urlencode('offline_access files.readwrite')
. '&response_type=code'
. '&redirect_uri=' . urlencode('https://auth.wpvivid.com/onedrive_v2/')
. '&state=' . urlencode(apply_filters('wpvivid_get_admin_url', '') . 'admin.php?page='.sprintf('%s-remote', apply_filters('wpvivid_white_label_slug', 'wpvivid')).'&action=wpvivid_pro_one_drive_finish_auth&sub_page=cloud_storage_onedrive&auth_id='.$auth_id)
. '&display=popup'
. '&locale=en';
header('Location: ' . esc_url_raw($url));
Fortunately, uploading to OneDrive seems to be functioning properly. We followed the official OneDrive documentation below to ensure a smooth upload process.
- According to the doc, create an upload session to upload files to OnDrive. Here are the code snippets:
$url='https://graph.microsoft.com/v1.0/me/drive/root:/'.$path.':/createUploadSession';
$args['method']='POST';
$args['headers']=array( 'Authorization' => 'bearer '.$this->options['token']['access_token'],'content-type' => 'application/json');
if(!is_null($body))
{
$args['body']=$body;
}
$args['timeout']=30;
$response=wp_remote_post($url,$args);
The session URL returned in the response:
https://my.microsoftpersonalcontent.com/personal/9fc61e00e002d0e3/_api/v2.0/drive/items/01AMXTOQ72H2ZYXNPRNZG2CAID634ROO4L/uploadSession?guid='b67d7587-1201-45e3-8fb5-5285cdc08012'&overwrite=True&rename=False&dc=0&tempauth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBfZGlzcGxheW5hbWUiOiJHcmFwaCIsImFwcGlkIjoiMDAwMDAwMDMtMDAwMC0wMDAwLWMwMDAtMDAwMDAwMDAwMDAwIiwiYXVkIjoi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l_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$upload_size=min($upload_size,$file_size-$uploaded);
if ($uploaded)
fseek($file_handle, $uploaded);
$headers = array(
"Content-Length: $upload_size",
"Content-Range: bytes $uploaded-$upload_end/".$file_size,
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_PUT => true,
CURLOPT_INFILE => $file_handle,
CURLOPT_INFILESIZE => $upload_size,
CURLOPT_RETURNTRANSFER=>true,
);
curl_setopt_array($curl, $options);
$response=curl_exec($curl);
Continue uploading file chunks using the 'nextExpectedRanges' provided in the response until the entire file is uploaded.
We attempted to download the backup from OneDrive following the official documentation (https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get_content?view=odsp-graph-online). Unfortunately, the download process failed.
Before proceeding, check if the file exists. Here's the code snippet for this step:
$url='https://graph.microsoft.com/v1.0/me/drive/root:/'.$folder.'/'.$file.'?$select=id,name,size';
if(empty($except_code))
{
$except_code=array(200,201,202,204,206);
}
$args['timeout']=$timeout;
$args['headers']['Authorization']= 'bearer '.$this->options['token']['access_token'];
$response=wp_remote_get($url,$args);
The '$url' in the code snippet is
https://graph.microsoft.com/v1.0/me/drive/root:/wpvividbackuppro/staging_untetheredbytinapayne_com/staging.untetheredbytinapayne.com_wpvivid-6639c8da6e73b_2024-05-07-01-23_backup_all.zip?$select=id,name,size
'$args' parameter is
{"timeout":30,"headers":{"Authorization":"bearer EwBoA8l6BAAUbDba3x2OMJElkF7gJ4z/VbCPEz0AAWP+nK+LDe9/gYinyOb9qXXwzcaMDpHlUCFgxpDiym9czdVDbAArgwmsGI+c+DqAyzgGEBf/QZ1hVSS/hFsPELNYdMwD8orcS+iadcYsRyOjIYA48NK6RVifg5VsQwLQvFOP/jO9Ho0mw17UoRrylJwNZ3uglKr4wL22Z1twPERWaBpdxaz1z2GHfZU+00HIL+QycgVLbQ+wvOrn6SZUVCAWQK+mPTy9hSFyZoitS1lGmUsKZ6D7ml9/5/FyVvVtXHt5nIGi9e2cdsRVy5XiXtbAuGz871TvBtCdgKE0n5Lj2JN/lKR/bEe4CjhkuttbrpBTP55l4ERb0/Ko5p8Od5MDZgAACH7rqvHkxM3OOAKgg4cMahuo1N0WkRW15qY1PgclkSaSBE6NEgecql5Koz+iUhBh457B7/dpyxncun89KFIoOZpxZn8WPY3epTqfFRLu1iSx1BgAB5sNqXC3Iqpsv5VpcHiySTwlctJHbbnHoEl8WVDRQgWxyjgQyXgEFV1u1kwvpywLZH6KRc++EV0/Axg9XnrW01xEI/0emiFndAmnSYnQ0meAjIXC3LDym+2KNUd05xF9dAeuW5lCJ+/tlpkPfcOY0uJhOMFHDaNCw7H6r/MhQGhHQVZeUm1NUCDi94RZ7xhNB0rDuI0PTKEWFcfR2TGQ/y/m+mYKikx0vsp/kFNp2QtIIbZL+JfvhB1b9n3ZlLT2j3fvZ6UNp5gODRzXLYeq8Wk3q+rFsJ4gG+2S3wqkjwyoB7muUbSWpHWXaFtVLu8RtTuoiu2YykfBy5M9+R0cCbeQ45P2ipD+ZK5WbIY0eUrRY+cPJ/K17UxcLBBTeCftJRW9KgYP1sFtfda1SXOm8PC5JW4YTP/AWcRmvqAIKaXJPKn92ZfBTlWQxxQNRxpJf8enPhl9p8+dj/sJx7M3vcCIUfXKtWt6LJld6ywI13oiSj0NZq3fPARJlrQvja5Z4Td55iW+oHIBKgOMzPCzxKXbSQ2O5mc3jYcY9iOewvehRF/fPS93aWyePaxApiWQ0bP13PqNTOBLWtzse1r1xQymF4B7FltTBrjVcV9PduAMe3u/bSewaQEf4Xgldv+KavdS8mDd+0ctRhV554YCcwI="}}
The request response is
{"headers":{},"body":"{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('aa%40alphalegacyfund.org')/drive/root(id,name,size)/$entity","@odata.etag":"\"{8BB33EFA-F1B5-4D6E-A101-03F6F9173B8B},3\"","id":"9FC61E00E002D0E3!s8bb33efaf1b54d6ea10103f6f9173b8b","name":"staging.untetheredbytinapayne.com_wpvivid-6639c8da6e73b_2024-05-07-01-23_backup_all.zip","size":78335490}","response":{"code":200,"message":"OK"},"cookies":[],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}
After confirming the file exisitance, start downlaoding files. Here are the codes snippet:
$url='https://graph.microsoft.com/v1.0/me/drive/root:/'.$path.'/'.$download_info['file_name'].':/content';
$headers['Range']="bytes=$downloaded_start-$downloaded_end";
$response=$this->remote_get($url,$headers,false,30);
The '$url' in the code snippet represent:
https://graph.microsoft.com/v1.0/me/drive/root:/wpvividbackuppro/staging_untetheredbytinapayne_com/staging.untetheredbytinapayne.com_wpvivid-6639c8da6e73b_2024-05-07-01-23_backup_all.zip:/content
$args parameter is
{"timeout":30,"headers":{"Authorization":"bearer EwBoA8l6BAAUbDba3x2OMJElkF7gJ4z/VbCPEz0AAWP+nK+LDe9/gYinyOb9qXXwzcaMDpHlUCFgxpDiym9czdVDbAArgwmsGI+c+DqAyzgGEBf/QZ1hVSS/hFsPELNYdMwD8orcS+iadcYsRyOjIYA48NK6RVifg5VsQwLQvFOP/jO9Ho0mw17UoRrylJwNZ3uglKr4wL22Z1twPERWaBpdxaz1z2GHfZU+00HIL+QycgVLbQ+wvOrn6SZUVCAWQK+mPTy9hSFyZoitS1lGmUsKZ6D7ml9/5/FyVvVtXHt5nIGi9e2cdsRVy5XiXtbAuGz871TvBtCdgKE0n5Lj2JN/lKR/bEe4CjhkuttbrpBTP55l4ERb0/Ko5p8Od5MDZgAACH7rqvHkxM3OOAKgg4cMahuo1N0WkRW15qY1PgclkSaSBE6NEgecql5Koz+iUhBh457B7/dpyxncun89KFIoOZpxZn8WPY3epTqfFRLu1iSx1BgAB5sNqXC3Iqpsv5VpcHiySTwlctJHbbnHoEl8WVDRQgWxyjgQyXgEFV1u1kwvpywLZH6KRc++EV0/Axg9XnrW01xEI/0emiFndAmnSYnQ0meAjIXC3LDym+2KNUd05xF9dAeuW5lCJ+/tlpkPfcOY0uJhOMFHDaNCw7H6r/MhQGhHQVZeUm1NUCDi94RZ7xhNB0rDuI0PTKEWFcfR2TGQ/y/m+mYKikx0vsp/kFNp2QtIIbZL+JfvhB1b9n3ZlLT2j3fvZ6UNp5gODRzXLYeq8Wk3q+rFsJ4gG+2S3wqkjwyoB7muUbSWpHWXaFtVLu8RtTuoiu2YykfBy5M9+R0cCbeQ45P2ipD+ZK5WbIY0eUrRY+cPJ/K17UxcLBBTeCftJRW9KgYP1sFtfda1SXOm8PC5JW4YTP/AWcRmvqAIKaXJPKn92ZfBTlWQxxQNRxpJf8enPhl9p8+dj/sJx7M3vcCIUfXKtWt6LJld6ywI13oiSj0NZq3fPARJlrQvja5Z4Td55iW+oHIBKgOMzPCzxKXbSQ2O5mc3jYcY9iOewvehRF/fPS93aWyePaxApiWQ0bP13PqNTOBLWtzse1r1xQymF4B7FltTBrjVcV9PduAMe3u/bSewaQEf4Xgldv+KavdS8mDd+0ctRhV554YCcwI=","Range":"bytes=0-2097151"}}
The request response is 401 Unauthorized
{"headers":{},"body":"{"error":{"code":"unauthenticated","message":"Unauthenticated"}}","response":{"code":401,"message":"Unauthorized"},"cookies":[],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}
Our plugin automatically checks for token expiration in every request. If a token is expired, it's refreshed to ensure uninterrupted service.
We've carefully followed the OneDrive Learn Documentation for both uploading and downloading files in our plugin. However, we're consistently encountering a 401 Unauthorized error during the download process. Any insights or suggestions on resolving this issue would be greatly appreciated.