J'essaie de télécharger un fichier image (taille de 12 Ko) dans le compte administrateur onedrive pour les entreprises utilisant l'API Web. Je peux obtenir le lecteur, root et childrenItems sans erreur.Téléchargement de fichier vers onedrive pour entreprise à l'aide du graphique Microsoft
var drive = FilesHelper.GetUserPersonalDrive();
var root = FilesHelper.GetUserPersonalDriveRoot();
var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);
mais lorsque je tente de télécharger le fichier d'image:
var new FileOnRoot= UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));
il lancer une exception:
{
"error": {
"code": "unauthenticated",
"message": "The caller is not authenticated.",
"innerError": {
"request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
"date": "2016-12-01T10:35:50"
}
}
}
Je permettais à peu près toutes les autorisations de lecture dans les autorisations déléguées pour l'API graphique et et toutes les autorisations d'application.
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)
{
}
classe 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);
}
}
classe 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);
}
}