1

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.

var drive = FilesHelper.GetUserPersonalDrive();
var root = FilesHelper.GetUserPersonalDriveRoot();
var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);

but when I try to upload the image file:

var new FileOnRoot=  UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));

it throw an exception:

{
  "error": {
    "code": "unauthenticated",
    "message": "The caller is not authenticated.",
    "innerError": {
      "request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
      "date": "2016-12-01T10:35:50"
    }
  }
}

I am allowing pretty much all read permissions under delegated permissions for Graph API and and all application permissions.

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 class

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 class

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);
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MOHAMMED
  • 11
  • 3

1 Answers1

0

You say that you are allowing ALL read permissions. However uploading a file to OneDrive requires a write permission. Please make sure that you have selected the Files.ReadWrite delegated permission (assuming you are using delegated flows). Please also make sure that you adopt a least privileged approach when choosing permissions. If you are using delegated flows, please don't select application permissions, and vice versa. You should follow the guidance here.

Also we have a series of samples available that might help you out, that call REST or call through a Microsoft Graph .Net client library, like this sample that has code for uploading files too. You can find our SDKs and samples on GitHub here.

Hope this helps,

Dan Kershaw - MSFT
  • 5,833
  • 1
  • 14
  • 23