title: Uploading file to onedrive for business using Microsoft graph
body: <p>I am trying to  upload an  image file (12 Kb size) to admin account onedrive for business using web API.
  I can get the drive, root and childrenItems with no error.</p>
  
  <pre><code>var drive = FilesHelper.GetUserPersonalDrive();
  var root = FilesHelper.GetUserPersonalDriveRoot();
  var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);
  </code></pre>
  
  <p>but when I try to upload the image file: </p>
  
  <pre><code>var new FileOnRoot=  UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));
  </code></pre>
  
  <p>it throw an exception:</p>
  
  <pre><code>{
    "error": {
      "code": "unauthenticated",
      "message": "The caller is not authenticated.",
      "innerError": {
        "request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
        "date": "2016-12-01T10:35:50"
      }
    }
  }
  </code></pre>
  
  <p>I am allowing pretty much all read permissions under delegated permissions for Graph API and and all application permissions.</p>
  
  <pre><code>private DriveItem UploadSampleFile(Drive drive, DriveItem newFolder, String filePath)
      {
          DriveItem result = null;
          Stream memPhoto = getFileContent(filePath);
  
          try
          {
              if (memPhoto.Length &gt; 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)
          {
      }
  </code></pre>
  
  <p>FilesHelper class</p>
  
  <pre><code>public static class FilesHelper
  {
      public static Drive GetUserPersonalDrive()
      {
          String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
              String.Format("{0}me/drive",
                  MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
  
          var drive = JsonConvert.DeserializeObject&lt;Drive&gt;(jsonResponse);
          return (drive);
      }
  
      public static DriveItem GetUserPersonalDriveRoot()
      {
          String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
              String.Format("{0}me/drive/root",
                  MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
  
          var folder = JsonConvert.DeserializeObject&lt;DriveItem&gt;(jsonResponse);
          return (folder);
      }
  
      public static List&lt;DriveItem&gt; 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&lt;DriveItemList&gt;(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&lt;DriveItem&gt;(jsonResponse);
  
          return (uploadedFile);
      }
  }
  </code></pre>
  
  <p>MicrosoftGraphHelper class</p>
  
  <pre><code>public static class FilesHelper
  {
      public static Drive GetUserPersonalDrive()
      {
          String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
              String.Format("{0}me/drive",
                  MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
  
          var drive = JsonConvert.DeserializeObject&lt;Drive&gt;(jsonResponse);
          return (drive);
      }
  
      public static DriveItem GetUserPersonalDriveRoot()
      {
          String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
              String.Format("{0}me/drive/root",
                  MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
  
          var folder = JsonConvert.DeserializeObject&lt;DriveItem&gt;(jsonResponse);
          return (folder);
      }
  
      public static List&lt;DriveItem&gt; 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&lt;DriveItemList&gt;(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&lt;DriveItem&gt;(jsonResponse);
  
          return (uploadedFile);
      }
  }
  </code></pre>