[英]Uploading file to onedrive for business using Microsoft graph
I am trying to upload an image file (12 Kb size) to admin account onedrive for business using web API. 我正在尝试使用Web API将图像文件(12 Kb大小)上传到管理员帐户onedrive for business。 I can get the drive, root and childrenItems with no error.
我可以得到驱动器,root和childrenItems没有错误。
var drive = FilesHelper.GetUserPersonalDrive();
var root = FilesHelper.GetUserPersonalDriveRoot();
var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);
but when I try to upload the image file: 但是当我尝试上传图像文件时:
var new FileOnRoot= UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));
it throw an exception: 它抛出异常:
{
"error": {
"code": "unauthenticated",
"message": "The caller is not authenticated.",
"innerError": {
"request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
"date": "2016-12-01T10:35:50"
}
}
}
I am allowing pretty much all read permissions under delegated permissions for Graph API and and all application permissions. 我在Graph API的授权权限和所有应用程序权限下允许几乎所有读取权限。
private DriveItem UploadSampleFile(Drive drive, DriveItem newFolder, String filePath)
{
DriveItem result = null;
Stream memPhoto = getFileContent(filePath);
try
{
if (memPhoto.Length > 0)
{
String contentType = "image/png";
result = FilesHelper.UploadFileDirect(drive.Id, newFolder.Id,
new DriveItem
{
File = new File { },
Name = filePath.Substring(filePath.LastIndexOf("\\") + 1),
ConflictBehavior = "rename",
},
memPhoto,
contentType);
}
}
catch (Exception ex)
{
}
FilesHelper class FilesHelper类
public static class FilesHelper
{
public static Drive GetUserPersonalDrive()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var drive = JsonConvert.DeserializeObject<Drive>(jsonResponse);
return (drive);
}
public static DriveItem GetUserPersonalDriveRoot()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive/root",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var folder = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (folder);
}
public static List<DriveItem> ListFolderChildren(String driveId, String folderId, Int32 numberOfItems = 100)
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}drives/{1}/items/{2}/children?$top={3}",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
folderId,
numberOfItems));
var driveItems = JsonConvert.DeserializeObject<DriveItemList>(jsonResponse);
return (driveItems.DriveItems);
}
public static Stream GetFileContent(String driveId, String fileId, String contentType)
{
Stream fileContent = MicrosoftGraphHelper.MakeGetRequestForStream(
String.Format("{0}drives/{1}/items/{2}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
fileId),
contentType);
return (fileContent);
}
public static DriveItem UploadFileDirect(String driveId, String parentFolderId,
DriveItem file, Stream content, String contentType)
{
var jsonResponse = MicrosoftGraphHelper.MakePutRequestForString(
String.Format("{0}drives/{1}/items/{2}/children/{3}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
parentFolderId,
file.Name),
content,
contentType);
var uploadedFile = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (uploadedFile);
}
}
MicrosoftGraphHelper class MicrosoftGraphHelper类
public static class FilesHelper
{
public static Drive GetUserPersonalDrive()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var drive = JsonConvert.DeserializeObject<Drive>(jsonResponse);
return (drive);
}
public static DriveItem GetUserPersonalDriveRoot()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive/root",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var folder = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (folder);
}
public static List<DriveItem> ListFolderChildren(String driveId, String folderId, Int32 numberOfItems = 100)
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}drives/{1}/items/{2}/children?$top={3}",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
folderId,
numberOfItems));
var driveItems = JsonConvert.DeserializeObject<DriveItemList>(jsonResponse);
return (driveItems.DriveItems);
}
public static Stream GetFileContent(String driveId, String fileId, String contentType)
{
Stream fileContent = MicrosoftGraphHelper.MakeGetRequestForStream(
String.Format("{0}drives/{1}/items/{2}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
fileId),
contentType);
return (fileContent);
}
public static DriveItem UploadFileDirect(String driveId, String parentFolderId,
DriveItem file, Stream content, String contentType)
{
var jsonResponse = MicrosoftGraphHelper.MakePutRequestForString(
String.Format("{0}drives/{1}/items/{2}/children/{3}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
parentFolderId,
file.Name),
content,
contentType);
var uploadedFile = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (uploadedFile);
}
}
You say that you are allowing ALL read permissions. 您说您允许所有读取权限。 However uploading a file to OneDrive requires a write permission.
但是,将文件上载到OneDrive需要写入权限。 Please make sure that you have selected the Files.ReadWrite delegated permission (assuming you are using delegated flows).
请确保您已选择Files.ReadWrite委派权限(假设您使用的是委托流)。 Please also make sure that you adopt a least privileged approach when choosing permissions.
在选择权限时,还请确保采用权限最低的方法。 If you are using delegated flows, please don't select application permissions, and vice versa.
如果您使用的是委派流程,请不要选择应用程序权限,反之亦然。 You should follow the guidance here .
你应该遵循这里的指导。
Also we have a series of samples available that might help you out, that call REST or call through a Microsoft Graph .Net client library, like this sample that has code for uploading files too. 此外,我们还提供了一系列可用的示例,可以帮助您,通过Microsoft Graph .Net客户端库调用REST或调用,就像这个包含上载文件代码的示例一样。 You can find our SDKs and samples on GitHub here .
你可以找到我们的SDK和样品在GitHub上这里 。
Hope this helps, 希望这可以帮助,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.