无法使用 Microsoft.Graph REST API 在根目录下创建新的 OneDrive 文件夹

Unable to create new OneDrive folder under root with Microsoft.Graph REST API

我可以在现有文件夹下创建文件夹,但不能在根目录下创建。我尝试使用根 ID 和路径语法的几种变体的 URL,例如"root:/./:",但其中 none 个创建了文件夹。

我想在 Microsoft.Graph REST API 的文档中查看在根目录下创建文件夹的示例。这可以节省很多时间。

感谢您的回答!

这是我的代码:

public static async Task<GameStorageItem> CreateFolderAsync(string parentId, string parentPath, 
                                                                string name)
    {
        var obj = new JObject
        {
            { "name", name },
            { "folder", new JObject() },
            { "@microsoft.graph.conflictBehavior", "fail" }
        };
        dynamic json;
        string content;
        if (parentId == "root")
        {
            content = await MicrosoftAccount.PerformHttpRequestAsync(HttpMethod.Get,
                                             $"me/drive", obj);
            json = JValue.Parse(content);
            parentId = json.id;

            //parentId = "root:./:";
        }
        content = await MicrosoftAccount.PerformHttpRequestAsync(HttpMethod.Post, $"me/drive/items/{parentId}/children", obj);
        json = JValue.Parse(content);
        DateTimeOffset created = json.createdDateTime;
        string id = json.id;
        var folder = new GameStorageFolder(name, $"{parentPath}/{name}", id, created, false);
        return folder;
    }
public static async Task<string> PerformHttpRequestAsync(HttpMethod method, string request, 
                                                             JObject json = null)
    {
        if (__authResult == null || await ValidateTokenAsync(5) == false)
        {
            try
            {
                await SignInAsync();
                __authResult = await __client.AcquireTokenSilent(scopes,
                                     __account).ExecuteAsync();
            }
            catch (MsalUiRequiredException)
            {
                //A MsalUiRequiredException happened on AcquireTokenSilentAsync. 
                //This indicates you need to call AcquireTokenAsync to acquire a token
                try
                {
                    //User must consent
                    __authResult = await __client.AcquireTokenInteractive(scopes)
                                         .ExecuteAsync();
                }
                catch (MsalException ex)
                {
                    //Error acquiring token
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                //Error acquiring token silently
                throw ex;
            }
        }
        var builder = new UriBuilder(__graphUrl + request);
        return await PerformHttpRequestWithTokenAsync(method, builder.Uri, 
                                                      __authResult.AccessToken, json);
    }
private static async Task<string> PerformHttpRequestWithTokenAsync(HttpMethod method, 
                                      Uri uri, string token, JObject json = null)
    {
        HttpResponseMessage response;
        var httpClient = new HttpClient();

        var request = new HttpRequestMessage(method, uri);
        if (json != null)
        {
            request.Content = new StringContent(json.ToString(), Encoding.UTF8, 
                                                "application/json");
        }
        //Add the token in Authorization header
        request.Headers.Authorization = 
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

        response = await httpClient.SendAsync(request);
        return await response.Content.ReadAsStringAsync();
    }

OneDrive 根目录资源

寻址 Microsoft Graph 根资源时,您的应用可以使用以下路径寻址 OneDrive 资源:

  1. /drives - 列出经过身份验证的可用驱动器资源 用户。
  2. /drives/{drive-id} - 通过 ID 访问特定驱动器。
  3. /drives/{drive-id}/root/children - 列出 a 的根目录中的项目 具体驱动器。
  4. /drive/items/{item-id} - 通过 ID 访问 driveItem。
  5. /drive/special/{special-id} - 通过已知文件夹访问已知文件夹 名字.
  6. /shares/{share-id} - 通过 driveItem 的 shareId 或 分享 URL

Path-based 在驱动器内寻址

driveItem 可以通过唯一标识符或该项目在驱动器层次结构中的位置(即用户路径)来寻址。在 API 请求中,可以使用冒号在 API 路径 space 和用户路径 space 之间切换。此语法对通过 API space.

寻址的任何 driveItem 有效

您还可以通过在文件系统路径 space 末尾使用冒号来转换回 API 路径 space。确保 URL 中的用户数据符合寻址和路径编码要求。

  1. /drive/root:/path/to/file - 通过路径访问一个driveItem 根。
  2. /drive/items/{item-id}:/path/to/file - 通过以下方式访问 driveItem 它相对于另一个项目的路径。
  3. /drive/root:/path/to/folder:/children - 列出 children 时 通过相对于驱动器根目录的路径访问。
  4. /drive/items/{item-id}:/path/to/folder:/children - 列表 children 当通过相对于另一个项目的路径访问时。

https://docs.microsoft.com/en-us/onedrive/developer/rest-api/?view=odsp-graph-online .

您有三种不同的选择 - 我只是将它们显示为请求,然后让您将其转换为代码:

选项 1 - POST 到 children

POST ../me/drive/root/children
{
  "name": "foo",
  "folder": {}
}

选项 2 - PUT 到 child

PUT ../me/drive/root/children/foo
{
  "folder": {}
}

选项 3 - PUT 到路径

PUT ../me/drive/root:/foo
{
  "folder": {}
}

请注意,所有这些 URL 都引用根目录,然后使用不同的机制在其下创建文件夹。