简体   繁体   English

使用Microsoft图表将文件上传到onedrive以进行业务

[英]Uploading file to onedrive for business using Microsoft graph

I am trying to upload an image file (12 Kb size) to admin account onedrive for business using web API. 我正在尝试使用Web API将图像文件(12 Kb大小)上传到管理员帐户onedrive for business。 I can get the drive, root and childrenItems with no error. 我可以得到驱动器,root和childrenItems没有错误。

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. 我在Graph 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 class 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 class 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);
    }
}

You say that you are allowing ALL read permissions. 您说您允许所有读取权限。 However uploading a file to OneDrive requires a write permission. 但是,将文件上载到OneDrive需要写入权限。 Please make sure that you have selected the Files.ReadWrite delegated permission (assuming you are using delegated flows). 请确保您已选择Files.ReadWrite委派权限(假设您使用的是委托流)。 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. 此外,我们还提供了一系列可用的示例,可以帮助您,通过Microsoft Graph .Net客户端库调用REST或调用,就像这个包含上载文件代码的示例一样。 You can find our SDKs and samples on GitHub here . 你可以找到我们的SDK和样品在GitHub上这里

Hope this helps, 希望这可以帮助,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OneDrive for Business的Microsoft Graph API是否可以过滤文件扩展名? - Does Microsoft Graph API for OneDrive for Business have the ability to filter on file extensions yet? 无法使用Microsoft Graph以全局管理员身份查询用户的OneDrive业务文件 - Cannot Query Users' OneDrive For Business Files As The Global Administrator using Microsoft Graph Microsoft Graph - 搜索 OneDrive - Microsoft Graph - Searching OneDrive 使用 Microsoft Graph 将新文件上传到 onedrive c# asp.net - Upload new file to onedrive using microsoft graph c# asp.net 使用 Microsoft Graph 将 C# Asp.Net Core 中的文件上传到 Sharepoint/OneDrive,无需用户交互 - Upload a file in C# Asp.Net Core to Sharepoint/OneDrive using Microsoft Graph without user interaction 使用 Microsoft Graph API 将文件上传到 MVC 应用程序中的 onedrive,权限错误 - Using Microsoft Graph API to upload file to onedrive in MVC Application, Error with permissions 带有Service / Daemon应用程序的Microsoft Graph CSharp SDK和OneDrive for Business - Quota facet返回null - Microsoft Graph CSharp SDK and OneDrive for Business with a Service/Daemon application - Quota facet returns null 如何在 c# 中使用 Microsoft Graph Api 上传到 OneDrive - How to upload to OneDrive using Microsoft Graph Api in c# 使用 Microsoft graph API 获取 OneDrive 中的所有文件夹 - Fetch all the folders in OneDrive using Microsoft graph API 如何使用OneDrive SDK更新存储在OneDrive(业务)上的文件的创建和/或最后修改时间 - How can I update the creation and/or the last modified time of a file stored on OneDrive (Business) using the OneDrive SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM