Ich versuche, eine Image-Datei (12 KB Größe) auf Admin-Konto onedrive für Unternehmen mit Web-API hochladen. Ich kann das Laufwerk, root und ChildrenItems ohne Fehler erhalten.Hochladen von Datei auf Onedrive für Unternehmen mit Microsoft Graph
var drive = FilesHelper.GetUserPersonalDrive();
var root = FilesHelper.GetUserPersonalDriveRoot();
var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);
aber wenn ich versuche, um die Bilddatei zu laden:
var new FileOnRoot= UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));
es eine Ausnahme:
{
"error": {
"code": "unauthenticated",
"message": "The caller is not authenticated.",
"innerError": {
"request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
"date": "2016-12-01T10:35:50"
}
}
}
ich erlaubt bin ziemlich alle Berechtigungen unter delegierte Berechtigungen für Graph API gelesen und alle Anwendungsberechtigungen.
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 Klasse
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 Klasse
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);
}
}