我跟随aspnet-snippets-sample的例子 和OneDriveTest.cs通过Microsoft Graph API将大文件上传到只有应用凭据的共享文件夹
并抛出以下异常:
"拒绝访问。您无权执行此操作或访问此资源。"
要初始化大型文件上传会话,请执行以下操作:
var uploadSession = await _graph
.Drives[DRIVEID]
.Items[ITEMID]
.ItemWithPath("newFile.docx")
.CreateUploadSession()
.Request().PostAsync();
driveId和itemId都有效。
我正在创建我的GraphClient,如下所示:
public MicrosoftGraphHelper()
{
_graph = new GraphServiceClient("https://graph.microsoft.com/beta",
new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", AccessToken);
return Task.FromResult(0);
}));
}
通过我的应用凭据和密钥获取访问令牌。在Azure中,我的应用程序通过Microsoft Graph对文件具有读/写权限。
另外,创建一个新的文件夹,如
POST https://graph.microsoft.com/beta/drives/DRIVEID/items/ITEMID/children
使用JSON
{
"name": "NewFolderTest",
"folder": { }
}
也可以。
这是上传任务
internal async Task<string> UploadLargeFile(HttpPostedFileBase file)
{
try
{
using (Stream fileStream = file.InputStream)
{
// Create the upload session. The access token is no longer required as you have session established for the upload.
// POST /v1.0/drive/root:/UploadLargeFile.bmp:/microsoft.graph.createUploadSession
var driveItems = await _graph.Drives[DRIVEID]
.Items[ITEMID]
.Children
.Request()
.GetAsync();
var uploadSession = await _graph.Drives[DRIVEID]
.Items[ITEMID].ItemWithPath("test.docx").CreateUploadSession()
.Request().PostAsync();
var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
var provider = new ChunkedUploadProvider(uploadSession, _graph, fileStream, maxChunkSize);
// Setup the chunk request necessities
var chunkRequests = provider.GetUploadChunkRequests();
var readBuffer = new byte[maxChunkSize];
var trackedExceptions = new List<Exception>();
DriveItem itemResult = null;
//upload the chunks
foreach (var request in chunkRequests)
{
// Do your updates here: update progress bar, etc.
// ...
// Send chunk request
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);
if (result.UploadSucceeded)
{
itemResult = result.ItemResponse;
}
}
// Check that upload succeeded
if (itemResult == null)
{
// Retry the upload
// ...
}
return "Success";
}
}
catch (ServiceException e)
{
// Debug.WriteLine("We could not upload the file: " + e.Error.Message);
return null;
}
}
一旦代码点击:
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);
试图请求:
"https://myapp.sharepoint.com:443/sites/dev/_api/v2.0/drives/DRIVEID/items/ITEMID/uploadSession"
被抛出。
我需要申请所需权限的内容和位置? Documentation是指Files.ReadWrite
Sharepoint是否需要Microsoft Graph API的其他权限?
在共享文件夹中,我可以看到一个新创建的tmp文件,其中包含0个字节。