C# 使用Microsoft graph将文件上载到onedrive for business
我正在尝试使用web API将一个图像文件(12 Kb大小)上载到管理帐户onedrive for business。 我可以毫无错误地获取驱动器、根目录和子目录项C# 使用Microsoft graph将文件上载到onedrive for business,c#,onedrive,microsoft-graph-api,azure-active-directory,C#,Onedrive,Microsoft Graph Api,Azure Active Directory,我正在尝试使用web API将一个图像文件(12 Kb大小)上载到管理帐户onedrive for business。 我可以毫无错误地获取驱动器、根目录和子目录项 var drive = FilesHelper.GetUserPersonalDrive(); var root = FilesHelper.GetUserPersonalDriveRoot(); var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id
var drive = FilesHelper.GetUserPersonalDrive();
var root = FilesHelper.GetUserPersonalDriveRoot();
var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);
但当我尝试上载图像文件时:
var new FileOnRoot= UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));
它会引发一个异常:
{
"error": {
"code": "unauthenticated",
"message": "The caller is not authenticated.",
"innerError": {
"request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
"date": "2016-12-01T10:35:50"
}
}
}
我允许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)
{
}
文件助手类
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);
}
}
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);
}
}
公共静态类文件帮助器
{
公共静态驱动器GetUserPersonalDrive()
{
字符串jsonResponse=MicrosoftGraphHelper.MakeGetRequestForString(
String.Format(“{0}me/drive”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri);
var drive=JsonConvert.DeserializeObject(jsonResponse);
返回(驱动);
}
公共静态驱动项GetUserPersonalDriveRoot()
{
字符串jsonResponse=MicrosoftGraphHelper.MakeGetRequestForString(
String.Format(“{0}me/drive/root”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri);
var folder=JsonConvert.DeserializeObject(jsonResponse);
返回(文件夹);
}
公共静态列表ListFolderChildren(String driveId、String folderId、Int32 numberOfItems=100)
{
字符串jsonResponse=MicrosoftGraphHelper.MakeGetRequestForString(
String.Format(“{0}驱动器/{1}/items/{2}/children?$top={3}”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
疯狂的,
福德里德,
数量);;
var driveItems=JsonConvert.DeserializeObject(jsonResponse);
返回(driveItems.driveItems);
}
公共静态流GetFileContent(字符串驱动ID、字符串文件ID、字符串内容类型)
{
Stream fileContent=MicrosoftGraphHelper.MakeGetRequestForStream(
String.Format(“{0}驱动器/{1}/items/{2}/content”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
疯狂的,
文件ID),
内容类型);
返回(文件内容);
}
公共静态DriveItem UploadFileDirect(字符串driveId、字符串parentFolderId、,
DriveItem文件、流内容、字符串内容类型)
{
var jsonResponse=MicrosoftGraphHelper.MakePutRequestForString(
String.Format(“{0}驱动器/{1}/items/{2}/children/{3}/content”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
疯狂的,
父辈,
文件名),
内容,,
内容类型);
var uploadedFile=JsonConvert.DeserializeObject(jsonResponse);
返回(上传文件);
}
}
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);
}
}
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);
}
}
公共静态类文件帮助器
{
公共静态驱动器GetUserPersonalDrive()
{
字符串jsonResponse=MicrosoftGraphHelper.MakeGetRequestForString(
String.Format(“{0}me/drive”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri);
var drive=JsonConvert.DeserializeObject(jsonResponse);
返回(驱动);
}
公共静态驱动项GetUserPersonalDriveRoot()
{
字符串jsonResponse=MicrosoftGraphHelper.MakeGetRequestForString(
String.Format(“{0}me/drive/root”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri);
var folder=JsonConvert.DeserializeObject(jsonResponse);
返回(文件夹);
}
公共静态列表ListFolderChildren(String driveId、String folderId、Int32 numberOfItems=100)
{
字符串jsonResponse=MicrosoftGraphHelper.MakeGetRequestForString(
String.Format(“{0}驱动器/{1}/items/{2}/children?$top={3}”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
疯狂的,
福德里德,
数量);;
var driveItems=JsonConvert.DeserializeObject(jsonResponse);
返回(driveItems.driveItems);
}
公共静态流GetFileContent(字符串驱动ID、字符串文件ID、字符串内容类型)
{
Stream fileContent=MicrosoftGraphHelper.MakeGetRequestForStream(
String.Format(“{0}驱动器/{1}/items/{2}/content”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
疯狂的,
文件ID),
内容类型);
返回(文件内容);
}
公共静态DriveItem UploadFileDirect(字符串driveId、字符串parentFolderId、,
DriveItem文件、流内容、字符串内容类型)
{
var jsonResponse=MicrosoftGraphHelper.MakePutRequestForString(
String.Format(“{0}驱动器/{1}/items/{2}/children/{3}/content”,
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
疯狂的,
父辈,
文件名),
内容,,
内容类型);
var uploadedFile=JsonConvert.DeserializeObject(jsonResponse);
返回(上传文件);
}
}
您说您允许所有读取权限。但是,将文件上载到OneDrive需要写入权限。请确保已选择Files.ReadWrite委托权限(假设您使用的是委托流)。另外,请确保在选择权限时采用权限最少的方法。如果您使用的是委托流,请不要选择应用程序权限,反之亦然。你应该遵循指南
此外,我们还提供了一系列示例,这些示例可能会对您有所帮助,可以调用REST,也可以通过Microsoft Graph.Net客户端库进行调用,就像这样,还有用于上载文件的代码。您可以在GitHub上找到我们的SDK和示例
希望这能有所帮助,您说您允许所有读取权限。但是,将文件上载到OneDrive需要写入权限。请确保已选择这些文件