Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Microsoft.Graph REST API在根目录下创建新的OneDrive文件夹_C#_Onedrive - Fatal编程技术网

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

C# 无法使用Microsoft.Graph REST API在根目录下创建新的OneDrive文件夹,c#,onedrive,C#,Onedrive,我可以在现有文件夹下创建文件夹,但不能在根目录下创建。我尝试了使用根id的URL和几种不同的路径语法,例如“root:/。/:”,但没有一个创建文件夹 我想看看在Microsoft.Graph REST API的docu中的根目录下创建文件夹的示例。这可以节省很多时间 谢谢你的回答 这是我的密码: public static async Task<GameStorageItem> CreateFolderAsync(string parentId, string parentPath

我可以在现有文件夹下创建文件夹,但不能在根目录下创建。我尝试了使用根id的URL和几种不同的路径语法,例如“root:/。/:”,但没有一个创建文件夹

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

谢谢你的回答

这是我的密码:

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();
    }
公共静态异步任务CreateFolderAsync(字符串parentId、字符串parentPath、,
字符串名称)
{
var obj=新作业对象
{
{“name”,name},
{“文件夹”,新作业对象()},
{“@microsoft.graph.conflictBehavior”,“fail”}
};
动态json;
字符串内容;
if(parentId==“根”)
{
content=等待MicrosoftAccount.PerformHttpRequestAsync(HttpMethod.Get,
$“我/驾驶”,obj);
json=JValue.Parse(内容);
parentId=json.id;
//parentId=“root:./:”;
}
content=wait MicrosoftAccount.performhttpprequestasync(HttpMethod.Post,$“me/drive/items/{parentId}/children”,obj);
json=JValue.Parse(内容);
DateTimeOffset created=json.createdDateTime;
字符串id=json.id;
var folder=newgamestoragefolder(名称,$“{parentPath}/{name}”,id,created,false);
返回文件夹;
}
公共静态异步任务PerformHttpRequestAsync(HttpMethod方法,字符串请求,
JObject(json=null)
{
if(uu authResult==null | | wait ValidateTokenAsync(5)==false)
{
尝试
{
等待信号同步();
__authResult=await\uuu client.AcquireTokenSilent(作用域,
__ExecuteAsync();
}
捕获(MsalUiRequiredException)
{
//AcquireTokenSilentAsync上发生MsalUiRequiredException。
//这表示需要调用AcquireTokenAsync来获取令牌
尝试
{
//用户必须同意
__authResult=await\uuuu客户端.AcquireTokenInteractive(作用域)
.ExecuteAsync();
}
渔获物(MSAlexex)
{
//获取令牌时出错
掷骰子;
}
}
捕获(例外情况除外)
{
//静默获取令牌时出错
掷骰子;
}
}
var builder=新的UriBuilder(uuu Grapurl+请求);
return wait wait performhtprequestwithtokenasync(方法,builder.Uri,
__authResult.AccessToken,json);
}
专用静态异步任务PerformHttpRequestWithTokenAsync(HttpMethod方法,
Uri,字符串标记,JObject json=null)
{
HttpResponseMessage响应;
var httpClient=新的httpClient();
var请求=新的HttpRequestMessage(方法,uri);
if(json!=null)
{
request.Content=newstringcontent(json.ToString(),Encoding.UTF8,
“应用程序/json”);
}
//在授权标头中添加令牌
request.Headers.Authorization=
新的System.Net.Http.Headers.AuthenticationHeaderValue(“承载者”,令牌);
响应=等待httpClient.SendAsync(请求);
return wait response.Content.ReadAsStringAsync();
}

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/{shares id}
    -通过其shares id或 共享URL
  • 驱动器内基于路径的寻址

    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
    -列出子项 按相对于另一项的路径访问时

  • .

    您有三个不同的选项-我将它们显示为请求,并让您将其转换为代码:

    备选案文1-向儿童邮寄

    POST ../me/drive/root/children
    {
      "name": "foo",
      "folder": {}
    }
    
    备选案文2——儿童贷款

    PUT ../me/drive/root/children/foo
    {
      "folder": {}
    }
    
    选项3-设置为路径

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

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

    谢谢您的回答。我已经在报纸上读过了