OneDrive / onedrive-sdk-csharp

OneDrive SDK for C#! https://dev.onedrive.com
Other
293 stars 143 forks source link

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll #203

Closed Onotseike closed 1 month ago

Onotseike commented 8 years ago

I am trying to integrate the Onedrive SDK to the UWP Portion of my Xamarin App, However in the Upload and Download code, I get the following exception when ever the following lines are reached:

using (var stream = await sqliteStorageFileFile.OpenStreamForReadAsync()) using (IRandomAccessStream appFileStream = await appFile.OpenAsync(FileAccessMode.ReadWrite)) Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll An Exception: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) mscorlib Here is the relevant code snippnets: UPLOAD:

   public async Task Upload(string filename)
{
    await InitializeClient();
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
    sqliteStorageFileFile = await localFolder.GetFileAsync(filename);

   try
    {
        using (var stream = await sqliteStorageFileFile.OpenStreamForReadAsync())
        {
            //Debug.WriteLine("QW"); Debug.WriteLine("QW"); Debug.WriteLine("QW");
            var item = await OneDriveClient.Drive.Special.AppRoot.ItemWithPath(sqliteStorageFileFile.Name)
                        .Content.Request().PutAsync<Item>(stream);
            Debug.WriteLine(item.Id + "Id of item");
        }

    }
    catch (Exception e)
    {
        Debug.WriteLine(e.Message + " " + e.Source);
    }

    return;
}

DOWNLOAD:

public async Task Download(string filename) { await InitializeClient(); try { var filebuilder = OneDriveClient.Drive.Special.AppRoot.ItemWithPath(filename); var backedUpFile = await filebuilder.Request().GetAsync(); var fileStream = await filebuilder.Content.Request().GetAsync(); //Byte Creation for fileStream byte[] fileBytes; IRandomAccessStream stream = fileStream.AsRandomAccessStream(); using (DataReader reader = new DataReader(stream)) { fileBytes = new byte[stream.Size]; await reader.LoadAsync((uint)stream.Size); reader.ReadBytes(fileBytes); }

        IStorageFile appFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename,
       CreationCollisionOption.OpenIfExists);

        using (IRandomAccessStream appFileStream = await appFile.OpenAsync(FileAccessMode.ReadWrite))
        {

            using (IOutputStream outputStream = appFileStream.GetOutputStreamAt(0))
            {
                using (DataWriter writer = new DataWriter(outputStream))
                {
                    writer.WriteBytes(fileBytes);
                    await writer.StoreAsync();
                    writer.DetachStream();
                }
                await outputStream.FlushAsync();
            }
        }
    }
    catch (Exception e )
    {
        Debug.WriteLine("An Exception: " + e.Message + "  " + e.Source + "  ");
    }
}

Please note the type of file I am trying to upload is a .db3 file (SQLite)

baywet commented 1 month ago

Thank you for reaching out and for your patience. This SDK is being officially deprecated. See #259 for more information