简体   繁体   English

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

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

I am able to create folders under existing folders but not under the root. 我能够在现有文件夹下创建文件夹,但不能在根目录下创建。 I tried URLs with the id of the root and with several variants of the path syntax eg "root:/./:", but none of them creates the folder. 我尝试使用root的id和路径语法的几种变体的URL,例如“root:/./:”,但是没有一个创建文件夹。

I would like to see an example of creating a folder under the root in the docu of the Microsoft.Graph REST API. 我想看一个在Microsoft.Graph REST API的文档中根目录下创建文件夹的示例。 This could save a lot of time. 这可以节省很多时间。

Thanks for any answer! 谢谢你的回答!

Here is my code: 这是我的代码:

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 root resources OneDrive根资源

When addressing a Microsoft Graph root resource, your app can address OneDrive resources using the following paths: 在寻址Microsoft Graph根资源时,您的应用可以使用以下路径寻址OneDrive资源:

  1. /drives - List drive resources available to the authenticated user. /drives - 列出经过身份验证的用户可用的驱动器资源。
  2. /drives/{drive-id} - Access a specific drive by its ID. /drives/{drive-id} - 按ID访问特定驱动器。
  3. /drives/{drive-id}/root/children - List items in the root of a specific drive. /drives/{drive-id}/root/children - 列出特定驱动器根目录中的项目。
  4. /drive/items/{item-id} - Access a driveItem by its ID. /drive/items/{item-id} - 按ID访问driveItem。
  5. /drive/special/{special-id} - Access a known folder by its known name. /drive/special/{special-id} - 按已知名称访问已知文件夹。
  6. /shares/{share-id} - Access a driveItem by its shareId or a sharing URL /shares/{share-id} - 通过其shareId或共享URL访问driveItem

Path-based addressing within a drive 驱动器中基于路径的寻址

A driveItem can be addressed by either a unique identifier or where that item exists in the drive's hierarchy (ie user path). 可以通过唯一标识符或驱动器层次结构中存在该项的位置(即用户路径)来寻址driveItem。 Within an API request, a colon can be used to shift between API path space and user path space. 在API请求中,冒号可用于在API路径空间和用户路径空间之间切换。 This syntax is valid for any driveItem addressed via the API space. 此语法对通过API空间寻址的任何driveItem有效。

You can also transition back to API path space by using a colon at the end of the file system path space. 您还可以通过在文件系统路径空间末尾使用冒号转换回API路径空间。 Ensure user data within the URL follows the addressing and path encoding requirements. 确保URL中的用户数据遵循寻址和路径编码要求。

  1. /drive/root:/path/to/file - Access a driveItem by path under the root. /drive/root:/path/to/file - 通过root下的路径访问driveItem。
  2. /drive/items/{item-id}:/path/to/file - Access a driveItem by its path relative to another item. /drive/items/{item-id}:/path/to/file - 通过相对于另一个项的路径访问driveItem。
  3. /drive/root:/path/to/folder:/children - List children when accessing by path relative to the drive root. /drive/root:/path/to/folder:/children - 按路径相对于驱动器根目录访问时列出子项。
  4. /drive/items/{item-id}:/path/to/folder:/children - List children when accessing by path relative to another item. /drive/items/{item-id}:/path/to/folder:/children - 按路径访问相对于另一个项目时列出子项。

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

You have three different options - I'll just show them as the request and let you translate it to code: 您有三个不同的选项 - 我只是将它们显示为请求,并允许您将其转换为代码:

Option 1 - POST to children 选项1 - 向儿童发布POST

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

Option 2 - PUT to child 选项2 - PUT给孩子

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

Option 3 - PUT to path 选项3 - PUT到路径

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

Note that all of these URLs reference the root, and then use different mechanisms to create a folder under it. 请注意,所有这些URL都引用根,然后使用不同的机制在其下创建文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM