Switched to Device code authentication

This commit is contained in:
2026-01-17 18:32:57 +01:00
parent 881ec1c0ec
commit 19c27c8ef0
4 changed files with 17 additions and 15 deletions

View File

@@ -13,15 +13,26 @@ public class OneDriveClient {
public OneDriveClient(ConfigData config) {
_config = config;
var credential = new ClientSecretCredential(_config.TenantId, _config.ClientId, _config.ClientSecret);
_client = new GraphServiceClient(credential);
var options = new DeviceCodeCredentialOptions {
TenantId = "consumers",
DeviceCodeCallback = (code, _) => {
Console.WriteLine(code.Message);
return Task.CompletedTask;
}
};
_client = new GraphServiceClient(new DeviceCodeCredential(options), ["Files.ReadWrite.All"]);
}
public async Task EnsureAuthenticated(CancellationToken token) {
await _client.Me.Drive.GetAsync(cancellationToken: token);
}
public async Task<UploadResult<DriveItem>> UploadFile(string filePath, CancellationToken token) {
var fileName = Path.GetFileName(filePath);
var remoteFilePath = _config.BackupUploadRoot.Trim('/') + '/' + fileName;
var defaultDrive = await _client.Users[_config.UserId].Drive.GetAsync(cancellationToken: token);
var defaultDrive = await _client.Me.Drive.GetAsync(cancellationToken: token);
var driveFile = _client.Drives[defaultDrive!.Id].Items[$"root:/{remoteFilePath}:"]!;
var uploadSession = await driveFile.CreateUploadSession.PostAsync(new CreateUploadSessionPostRequestBody {
@@ -40,7 +51,7 @@ public class OneDriveClient {
}
public async Task<int> DeleteOldFiles(CancellationToken token) {
var defaultDrive = await _client.Users[_config.UserId].Drive.GetAsync(cancellationToken: token);
var defaultDrive = await _client.Me.Drive.GetAsync(cancellationToken: token);
var remoteFolder = _config.BackupUploadRoot.Trim('/');
var backupFiles = await _client.Drives[defaultDrive!.Id]