1. 首先配置 登录 Azure 进入 https://portal.azure.com/ 配置好之后获取,客户端ID、租户id、客户密码、
配置地址 可以按照 https://www.cnblogs.com/xds881010/p/13751037.html 进行配置。
通过NuGet安装
引用using Microsoft.Graph 4.47.0 版本
引用 using Microsoft.Identity.Client 4.72.1
1. 获取token命令
public class OneDriveUploader { private readonly string _clientId; private readonly string _tenantId; private readonly string _clientSecret; private readonly bool _isChinaCloud; public OneDriveUploader(string clientId, string tenantId, string clientSecret, bool isChinaCloud = false) { _clientId = clientId; _tenantId = tenantId; _clientSecret = clientSecret; _isChinaCloud = isChinaCloud; } public async Task UploadFileAsync(string localFilePath, string oneDriveFolderPath = null) { try { DriveItem uploadedFile; //var graphClient = GetGraphClient(); // 1. 获取认证令牌 var graphClient = await GetAuthenticatedClient(); using var fileStream = System.IO.File.OpenRead(localFilePath); var fileName = Path.GetFileName(localFilePath); //https://microsoftgraph.chinacloudapi.cn/Files.ReadWrite.All var drive = await graphClient.Users["cameron.xu@jfcchina.com"].Drive .Request() .GetAsync(); // 确定上传路径 var uploadPath = string.IsNullOrEmpty(oneDriveFolderPath) ? fileName : $"{oneDriveFolderPath}/{fileName}"; // 小文件直接上传 (<4MB) if (fileStream.Length < 4 * 1024 * 1024) { uploadedFile = await graphClient.Users["上传到对应站点下邮箱/对象ID"].Drive.Root .ItemWithPath(uploadPath) .Content .Request() .PutAsync<DriveItem>(fileStream); } else { // 大文件分块上传 var uploadSession = await graphClient.Users[""].Drive.Root .ItemWithPath(uploadPath) .CreateUploadSession() .Request() .PostAsync(); var uploadTask = new LargeFileUploadTask<DriveItem>( uploadSession, fileStream, 320 * 1024); // 320KB 分块 var uploadResult = await uploadTask.UploadAsync(); if (!uploadResult.UploadSucceeded) { throw new Exception("大文件上传失败"); } uploadedFile = uploadResult.ItemResponse; } // 2. 创建编辑链接 var permission = await graphClient.Users["cameron.xu@jfcchina.com"].Drive.Items[uploadedFile.Id] .CreateLink("edit", "organization", expirationDateTime: DateTimeOffset.Now.AddDays(7)) .Request() .PostAsync(); var ss = permission.Link.WebUrl; var url = uploadedFile.WebUrl; Console.WriteLine($"文件上传成功!ID:"); } catch (Exception ex) { Console.WriteLine($"上传失败: {ex.Message}"); throw; } } private async Task<GraphServiceClient> GetAuthenticatedClient() { // 确定云环境配置 string authority = _isChinaCloud ? $"https://login.partner.microsoftonline.cn/{_tenantId}" : $"https://login.microsoftonline.com/{_tenantId}"; string graphScope = _isChinaCloud ? "https://microsoftgraph.chinacloudapi.cn/.default" : "https://graph.microsoft.com/.default"; string graphEndpoint = _isChinaCloud ? "https://microsoftgraph.chinacloudapi.cn/v1.0" : "https://graph.microsoft.com/v1.0"; // 创建机密客户端应用 var app = ConfidentialClientApplicationBuilder .Create(_clientId) .WithClientSecret(_clientSecret) .WithAuthority(new Uri(authority)) .Build(); // 获取访问令牌 var authResult = await app.AcquireTokenForClient(new[] { graphScope }) .ExecuteAsync(); // 1. 获取认证配置 var authProvider = new DelegateAuthenticationProvider(async (request) => { request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken); }); // 2. 创建Graph客户端并设置终结点 var graphClient = new GraphServiceClient(authProvider) { BaseUrl = _isChinaCloud ? "https://microsoftgraph.chinacloudapi.cn/v1.0" : "https://graph.microsoft.com/v1.0" }; return graphClient; } }
// 替换为你的实际凭据 var clientId = ""; var tenantId = ""; var clientSecret = ""; var isChinaCloud = true; var uploader = new OneDriveUploader(clientId, tenantId, clientSecret, isChinaCloud); // 上传小文件 await uploader.UploadFileAsync(@"F:\ConsoleApp3\example.docx", "Work/Documents");
这个保存是对应的下面没有站点。需要开放

浙公网安备 33010602011771号