【问题标题】:Shared OneDrive Directory From Application Authentication来自应用程序身份验证的共享 OneDrive 目录
【发布时间】:2019-09-20 05:00:14
【问题描述】:

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

我一直在研究 Microsoft.Graph sdk 的多个示例。我已经能够使用应用程序和 ConfidentialClient 工作流程进行身份验证,但我不确定如何在 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"
}

【问题讨论】:

    标签: c# microsoft-graph-api onedrive microsoft-graph-sdks


    【解决方案1】:

    所以我能够通过迂回的方式到达那里,如果有人知道更简单的方式,我真的很感激。以下是我采取的步骤:

    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();
    

    收到回复后,我记下了驱动器的“driveId”。然后,当我使用我的机密客户端进行身份验证时,我可以在我的请求中指定驱动器:

    await this.graphClient.Drives["<driveId from 1>"].Root.Request().Expand(expandValue).GetAsync()
    

    我想知道是否有更简单的方法可以找到那些 driveId,比如从 sharepoint 站点?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2019-09-29
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多