我能够在现有文件夹下创建文件夹,但不能在根目录下创建文件夹。我尝试了具有根ID和路径语法的多种变体的网址,例如“ root:/./:”,但都不创建该文件夹。
我想看一个在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();
}
答案 0 :(得分:0)
OneDrive根资源
寻址Microsoft Graph根资源时,您的应用程序可以使用以下路径寻址OneDrive资源:
/drives
-列出已验证的可用驱动器资源
用户。/drives/{drive-id}
-通过其ID访问特定的驱动器。/drives/{drive-id}/root/children
-在列表的根目录中列出项目
特定驱动器。/drive/items/{item-id}
-通过其ID访问driveItem。/drive/special/{special-id}
-通过已知文件夹访问已知文件夹
名称。/shares/{share-id}
-通过其shareId或驱动器访问driveItem
共享网址驱动器中基于路径的寻址
driveItem可以通过唯一标识符或驱动器层次结构(即用户路径)中该项目所在的位置来寻址。在API请求中,冒号可用于在API路径空间和用户路径空间之间切换。此语法对通过API空间寻址的任何driveItem有效。
您还可以通过在文件系统路径空间末尾使用冒号来过渡回API路径空间。确保URL中的用户数据符合寻址和路径编码要求。
/drive/root:/path/to/file
-通过以下路径访问driveItem
根。 /drive/items/{item-id}:/path/to/file
-通过访问driveItem
它相对于另一个项目的路径。/drive/root:/path/to/folder:/children
-列出孩子的时间
通过相对于驱动器根目录的路径进行访问。/drive/items/{item-id}:/path/to/folder:/children
-列出子级
通过相对于另一个项目的路径访问时。https://docs.microsoft.com/en-us/onedrive/developer/rest-api/?view=odsp-graph-online
答案 1 :(得分:0)
您有三种不同的选择-我将把它们显示为请求,然后让您将其翻译为代码:
选项1-POST给孩子
POST ../me/drive/root/children
{
"name": "foo",
"folder": {}
}
选项2-放到孩子身上
PUT ../me/drive/root/children/foo
{
"folder": {}
}
选项3-放置到路径
PUT ../me/drive/root:/foo
{
"folder": {}
}
请注意,所有这些URL都引用根,然后使用不同的机制在根下创建一个文件夹。