Я могу создавать папки в существующих папках, но не в корне. Я пробовал 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();
}