Android - 通过 CSOM 或 REST 连接到 MS Sharepoint 导致本地文件系统 UnauthorizedAccessException

问题描述

我正在用 C#(通过 Unity)为 Android 设备开发一个应用程序,该应用程序链接到 Microsoft 的 SharePoint 系统以获取文件和信息。 C# 应用程序可以通过 CSOM api 或 REST api 连接到 SharePoint。这适用于测试和 Windows。

除了一个关键部分之外,一切都在成功运行:

在实际的 Android 平板电脑(Samsung Tab 8 - Android 10)上运行时,它返回错误

Error Unity UnauthorizedAccessException: Access to the path "/assets" is denied.

这发生在 CSOM clientContext.ExecuteQuery();函数运行。

所以我尝试使用 REST 方法。但这会在以下位置返回相同的 AccessExeption:

data = client.DownloadData(endpointUri);

这些命令对于两种检索信息的方法都是必不可少的。我只能想象它正在存储某种临时信息,因为此时它实际上并不需要在这里保存任何内容,只需将信息下载到一个临时变量中即可。对文件位置的实际写入稍后进行。

我还测试了 'Directory("/test")',它返回相同的 AccessExeption。 应用程序上的其他所有内容都会写入 Application.PersistentDataPath 等并且工作正常。

有没有办法让 Android 内部文件系统的这一部分可以访问? 我已经完成了清单中的 android.permission.WRITE_EXTERNAL_STORAGE" 和 android:requestLegacyExternalStorage="true" 。 当我运行应用程序时会请求并授予它们,并且所有其他读写都可以正常工作到其他位置(如照片、Android/数据/应用程序名称/文件等)。

我可以创建一个测试图像文件并使用此文件保存:

System.IO.File.WriteallBytes(Application.persistentDataPath +  Path.DirectorySeparatorChar + "Local saves" + separator + "image.jpg",bytes);

这个图像文件文件夹出现在 Android 设备上(我已经做了很多次这种事情),所以它肯定可以写入 Application.persistentDataPath

Directory.CreateDirectory、Directory.CreateFile、System.IO.File.WriteallBytes Application.persistentDataPath 一切正常。

出于某种原因,REST 代码和/或 CSOM 的 executeQuery() 中的某些内容正在尝试访问其他地方,但它们不请求文件路径,那么我该如何使它们工作?

非常感谢任何建议,我花了几天的大部分时间试图解决这个问题,但没有任何效果

用于在测试中下载文件并在 Windows 上下载文件的 REST 代码。 (最后几行被注释掉,以确定哪个位引发了错误

    public static void DownloadFileViaRestAPI(string webUrl,ICredentials credentials,string documentLibName,string fileName,string path)
    {
        webUrl = webUrl.EndsWith("/") ? webUrl.Substring(0,webUrl.Length - 1) : webUrl;
        string webRelativeUrl = null;
        if (webUrl.Split('/').Length > 3)
        {
            webRelativeUrl = "/" + webUrl.Split(new char[] { '/' },4)[3];
        }
        else
        {
            webRelativeUrl = "";
        }
        
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED","f");
            client.Credentials = credentials;
            Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + webRelativeUrl + "/" + documentLibName + "/" + fileName + "')/$value");
            //string result = client.DownloadString(endpointUri);
            byte[] data = client.DownloadData(endpointUri);
            //char separator = Path.DirectorySeparatorChar;
            
            //FileStream outputStream = new FileStream(SharedData.appRoot + "Local saves" + separator + fileName,FileMode.OpenorCreate | FileMode.Append,FileAccess.Write,FileShare.None);
            //outputStream.Write(data,data.Length);
            //outputStream.Flush(true);
            //outputStream.Close();
        }
        
    }

byte[] data = client.DownloadData(endpointUri); 处的错误 :

2021/03/15 15:54:10.428 8826 8881 Error Unity UnauthorizedAccessException: Access to the path "/assets" is denied.
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x0004b] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.Directory.CreateDirectory (System.String path) [0x00094] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.DirectoryInfo.Create () [0x00000] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at (wrapper remoting-invoke-with-check) System.IO.DirectoryInfo.Create()
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00023] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.Directory.CreateDirectory (System.String path) [0x00094] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.DirectoryInfo.Create () [0x00000] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at (wrapper remoting-invoke-with-check) System.IO.DirectoryInfo.Create()
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.Directory.CreateDirectoriesInternal (System.String path) [0x00023] in <2b3a3162be434770b7a4fac8b896e90c>:0 
2021/03/15 15:54:10.428 8826 8881 Error Unity   at System.IO.Directory.Crea

在某些方面这是有道理的。 CSOM 或 REST 都会在下载文件时创建某种临时空间。但是如何改变它的位置或允许他们访问? 任何帮助表示赞赏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)