2016-12-01 1 views
1

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); 
    } 
} 

Répondre

0

Vous dites que vous autorisez ALL lire autorisations. Toutefois, le téléchargement d'un fichier vers OneDrive nécessite une autorisation write. Assurez-vous que vous avez sélectionné l'autorisation déléguée Files.ReadWrite (en supposant que vous utilisez des flux délégués). Veillez également à adopter une approche moins privilégiée lors du choix des autorisations. Si vous utilisez des flux délégués, ne sélectionnez pas les autorisations d'application, et vice versa. Vous devez suivre les instructions here.

Nous avons également une série d'exemples disponibles qui peuvent vous aider, qui appellent REST ou appellent à travers une bibliothèque client Microsoft Graph .Net, comme ceci sample qui a le code pour télécharger des fichiers aussi. Vous pouvez trouver nos SDK et échantillons sur GitHub here.

Espérons que cela aide,