2016-12-01 69 views
1

我想上傳一個圖像文件(12 Kb大小)管理帳戶onedrive使用Web API的業務。 我可以得到沒有錯誤的驅動器,根和childrenItems。使用微軟圖上傳文件到onedrive業務使用微軟圖

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" 
    } 
    } 
} 

我允許幾乎下圖形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類

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類

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

回答

0

你說,你是讓所有閱讀權限。但是,將文件上傳到OneDrive需要編寫權限。請確保您選擇了Files.ReadWrite委託權限(假設您使用委託流)。請確保您在選擇權限時採用最少特權的方法。如果您使用委託流,請不要選擇應用程序權限,反之亦然。你應該遵循指導here

此外,我們還有一系列可用的樣本,可能會幫助您調用REST或通過Microsoft Graph .Net客戶端庫調用,例如sample也有上傳文件的代碼。您可以在GitHub here上找到我們的SDK和樣本。

希望這會有所幫助,