我有一个标准URL列表,我正在从SharePoint和Business OneDrive文件夹中下载文件。 URL如下: https://tenant-my.sharepoint.com/personal/bob_smith_tenant_onmicrosoft_com/Documents/Microsoft小组聊天文件/file.csv
https://tenant.sharepoint.com/sites/Development/Shared%20Documents/General/file2.pdf
我没有使用Graph SDK来直接下载文件的方法。我已经使用CSOM成功下载了SharePoint文件,但我更喜欢使用Graph,以便它可以处理两种文件类型。有人知道这是否可能吗?预先感谢
答案 0 :(得分:2)
在OneDrive API中通过网址寻址资源时,shares
endpoint可以解决。在这种情况下,按完整网址下载文件的流程可能包含以下步骤:
shares
端点/shares/{shareIdOrEncodedSharingUrl}/driveitem/content
示例
const string fileFullUrl = "https://contoso-my.sharepoint.com/personal/jdoe_contoso_onmicrosoft_com/documents/sample.docx";
var sharedItemId = UrlToSharingToken(fileFullUrl);
var requestUrl = $"{graphClient.BaseUrl}/shares/{sharedItemId}/driveitem/content";
var message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
var response = await graphClient.HttpProvider.SendAsync(message);
var bytesContent = await response.Content.ReadAsByteArrayAsync();
System.IO.File.WriteAllBytes("sample.docx", bytesContent); //save into local file
如何将URL转换为共享令牌
以下代码段演示了如何将URL转换为共享令牌(改编自here)
static string UrlToSharingToken(string inputUrl)
{
var base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(inputUrl));
return "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');
}