c# - 无法使用 Microsoft.Graph REST API 在根目录下创建新的 OneDrive 文件夹
问题描述
我可以在现有文件夹下创建文件夹,但不能在根目录下创建文件夹。我尝试了带有根 id 和路径语法的几种变体的 URL,例如“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();
}
解决方案
您有三个不同的选项 - 我将它们显示为请求并让您将其转换为代码:
选项 1 - 发布给孩子
POST ../me/drive/root/children
{
"name": "foo",
"folder": {}
}
选项 2 - PUT 给孩子
PUT ../me/drive/root/children/foo
{
"folder": {}
}
选项 3 - PUT 到路径
PUT ../me/drive/root:/foo
{
"folder": {}
}
请注意,所有这些 URL 都引用根目录,然后使用不同的机制在其下创建文件夹。
推荐阅读
- python - 注册时如何在 Django 中将用户设置为非活动状态?
- audio - 如何将视频文件与音频文件合并并保持创建时间?
- asp.net-core - 构造函数是不明确的错误.NET Core Web API
- unity3d - 在Unity3D中移动到RaycastHit点时使对象与某些其他对象发生碰撞?
- html - 带有 Canvas 的 HTML 问答游戏
- flutter - Flutter 中的自定义货币 TextField
- angular - Angular 8 - Headless Chrome 运行两次,第二次失败
- html - 如何在引导卡的左侧添加颜色
- javascript - 取消confirm()依然提交
- python - Python3 - 从需要登录的网站抓取数据 - 我可以使用当前登录的浏览器的用户代理吗?