C# 来自应用程序身份验证的共享OneDrive目录

C# 来自应用程序身份验证的共享OneDrive目录,c#,microsoft-graph-api,onedrive,microsoft-graph-sdks,C#,Microsoft Graph Api,Onedrive,Microsoft Graph Sdks,我有一个应用程序,可以处理大量数据并生成一些结果。目前,应用程序将结果发送电子邮件,但这可能会很麻烦,而且电子邮件可能会太大,因此我希望应用程序将结果存储到共享的OneDrive文件夹中。应用程序在没有用户交互的情况下运行 我一直在研究Microsoft.Graph sdk的多个示例。我已经能够使用应用程序和机密客户端工作流进行身份验证,但我不确定如何在OneDrive中查找/访问共享目录。当前,仅根驱动器请求不会返回任何子项或任何有用的内容。当我使用API以我的用户身份登录时,我可以访问共享驱

我有一个应用程序,可以处理大量数据并生成一些结果。目前,应用程序将结果发送电子邮件,但这可能会很麻烦,而且电子邮件可能会太大,因此我希望应用程序将结果存储到共享的OneDrive文件夹中。应用程序在没有用户交互的情况下运行

我一直在研究Microsoft.Graph sdk的多个示例。我已经能够使用应用程序和机密客户端工作流进行身份验证,但我不确定如何在OneDrive中查找/访问共享目录。当前,仅根驱动器请求不会返回任何子项或任何有用的内容。当我使用API以我的用户身份登录时,我可以访问共享驱动器,但那是使用我的用户的共享链接。我是否需要为应用程序生成共享链接,还是不与用户绑定?或者是否有其他方法可以找到共享驱动器

下面是创建我的GraphServiceClient的代码:

public static GraphServiceClient GetAuthenticatedClient()
        {
            if (graphClient == null)
            {
                ConfidentialClientApp = ConfidentialClientApplicationBuilder.Create(clientId).WithTenantId(FormBrowser.MsaTenantId).WithClientSecret(FormBrowser.MsaClientSecret).Build();
                ClientCredentialProvider authProvider = new ClientCredentialProvider(ConfidentialClientApp);
                graphClient = new GraphServiceClient(authProvider);
            }
            return graphClient;
        }
然后,我尝试了一些不同的调用,以确保其正确验证:

   //var shares = await this.graphClient.Shares[encodedUrl].Root.Request().Expand(expandValue).GetAsync();
                    //ProcessFolder(shares);
                    var drive = graphClient.Drive.Request().GetAsync();
                    ProcessFolder(await this.graphClient.Drive.Root.Request().Expand(expandValue).GetAsync());
下面是来自Drive.Root请求的JSON响应示例:

{
    "createdDateTime": "2013-11-07T19:59:00+00:00",
    "lastModifiedDateTime": "2019-09-15T02:12:23+00:00",
    "name": "root",
    "webUrl": "https://<company>.sharepoint.com/Documents",
    "fileSystemInfo": {
        "createdDateTime": "2013-11-07T19:59:00+00:00",
        "lastModifiedDateTime": "2019-09-15T02:12:23+00:00"
    },
    "folder": {
        "childCount": 0
    },
    "parentReference": {
        "driveId": "<stuff>",
        "driveType": "documentLibrary"
    },
    "root": {

    },
    "size": 0,
    "children": [

    ],
    "thumbnails": [

    ],
    "id": "<stuff>",
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drive/root(thumbnails(),children(thumbnails()))/$entity",
    "children@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drive/root/children(thumbnails())",
    "thumbnails@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drive/root/thumbnails",
    "responseHeaders": {
        "Transfer-Encoding": [
            "chunked"
        ],
        "Vary": [
            "Accept-Encoding"
        ],
        "request-id": [
            "<id>"
        ],
        "client-request-id": [
            "<id>"
        ],
        "x-ms-ags-diagnostic": [
            "{\"ServerInfo\":{\"DataCenter\":\"North Central US\",\"Slice\":\"SliceC\",\"Ring\":\"1\",\"ScaleUnit\":\"000\",\"RoleInstance\":\"<stuff>\",\"ADSiteName\":\"<stuff>\"}}"
        ],
        "OData-Version": [
            "4.0"
        ],
        "Duration": [
            "259.0577"
        ],
        "Strict-Transport-Security": [
            "max-age=31536000"
        ],
        "Cache-Control": [
            "private"
        ],
        "Date": [
            "Thu, 19 Sep 2019 14:06:27 GMT"
        ]
    },
    "statusCode": "OK"
}

所以我能够绕道到达那里,如果有人知道一个更简单的方法,我会非常感激。以下是我采取的步骤:

1与我的用户进行了身份验证,并使用共享url加载了信息:

 string sharingUrl = "<url>";
 string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
 string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');                        
 var shares = await this.graphClient.Shares[encodedUrl].Root.Request().Expand(expandValue).GetAsync();
当我得到回复后,我记下了驱动器的驱动器ID。然后,当我使用机密客户端进行身份验证时,我可以在请求中指定驱动器:

await this.graphClient.Drives["<driveId from 1>"].Root.Request().Expand(expandValue).GetAsync()
我想知道是否有更简单的方法来查找那些driveId,比如从sharepoint网站

另外,当我从Sharepoint获取共享链接时,如果我将链接从特定的人切换到中的人,那么我可以使用共享获取驱动器项目