π OneDriveRestApi
A Dart package that provides convenient access to the OneDrive API, built on top of oauth2restclient.
β¨ Features
- π OAuth2 authentication via
oauth2restclient - π List, upload, download, copy, move, and delete OneDrive files
- π List and create folders
- π‘ Easy access to OneDrive Streams and metadata
- π Supports both personal and business OneDrive
- π€ Path-based API for intuitive file operations
π¦ Installation
Add to your pubspec.yaml:
dependencies:
onedrive_rest_api: ^0.0.2
π Getting Started
import 'package:onedrive_rest_api/onedrive_rest_api.dart';
void main() async {
final account = OAuth2Account();
// Microsoft OAuth2 provider λ±λ‘
account.addProvider(Microsoft(
clientId: "YOUR_CLIENT_ID",
redirectUri: "YOUR_REDIRECT_URI",
scopes: [
"User.Read",
"Files.ReadWrite.All",
"Files.Read.All",
"openid",
"email",
"offline_access",
],
));
// λ‘κ·ΈμΈ λλ ν ν° λ‘λ
final token = await account.newLogin("microsoft");
final client = await account.createClient(token);
// API μΈμ€ν΄μ€ μμ±
final onedrive = OneDriveRestApi(client);
// λλΌμ΄λΈ μ 보 κ°μ Έμ€κΈ°
final drive = await onedrive.getDrive();
print("Drive ID: ${drive.id}");
// λ£¨νΈ ν΄λ νμΌ λͺ©λ‘ μ‘°ν
final items = await onedrive.listChildren("/");
for (final item in items.value) {
print("${item.name} (${item.id})");
}
}
π Example Operations
- List Files:
// λ£¨νΈ λλ ν 리 λ΄μ© μ‘°ν
await onedrive.listChildren("/", top: 20);
// νΉμ ν΄λ λ΄μ© μ‘°ν
await onedrive.listChildren("/Documents", top: 20);
- Upload File:
// 루νΈμ νμΌ μ
λ‘λ
await onedrive.upload("/example.txt", fileStream);
// νΉμ ν΄λμ νμΌ μ
λ‘λ
await onedrive.upload("/Documents/report.pdf", fileStream);
- Create Folder:
// 루νΈμ ν΄λ μμ±
await onedrive.createFolder("/New Folder");
// νΉμ ν΄λ μμ νμ ν΄λ μμ±
await onedrive.createFolder("/Documents/Work");
- Download File:
// νμΌ λ€μ΄λ‘λ
final stream = await onedrive.download("/Documents/file.txt");
- Copy File:
// νμΌ λ³΅μ¬ (κ°μ μ΄λ¦)
await onedrive.copy("/Documents/file.txt", "/Backup/file.txt");
// νμΌ λ³΅μ¬ (λ€λ₯Έ μ΄λ¦)
await onedrive.copy("/Documents/file.txt", "/Backup/file_copy.txt");
// β οΈ λ³΅μ¬λ λΉλκΈ° μμ
μ
λλ€. λ³΅μ¬ μλ£κΉμ§ μκ°μ΄ 걸릴 μ μμ΅λλ€.
- Move/Rename File:
// νμΌ μ΄λ (κ°μ μ΄λ¦)
await onedrive.move("/Documents/file.txt", "/Pictures/file.txt");
// νμΌ μ΄λνλ©΄μ μ΄λ¦ λ³κ²½
await onedrive.move("/Documents/file.txt", "/Pictures/new_name.txt");
// κ°μ ν΄λμμ μ΄λ¦λ§ λ³κ²½
await onedrive.move("/Documents/file.txt", "/Documents/new_name.txt");
- Delete File:
// νμΌ μμ
await onedrive.delete("/Documents/file.txt");
// ν΄λ μμ
await onedrive.delete("/Documents/Old Folder");
π€ Path-based API
- λͺ¨λ μ£Όμ ν¨μλ κ²½λ‘(path)λ§ λ°μ΅λλ€. (ID κΈ°λ° μλ)
- 루νΈλ‘ 볡μ¬/μ΄λ/μμ± μ κ²½λ‘λ
/λλ""(λΉ λ¬Έμμ΄)λ‘ μ²λ¦¬νλ©΄ λ©λλ€. - copy/moveλ λ°νκ°μ΄ μμΌλ©°, copyλ λΉλκΈ° μμ μ΄λ―λ‘ λ°λ‘ 볡μ¬λ³Έμ΄ 보μ΄μ§ μμ μ μμ΅λλ€.
κ²½λ‘ μμ
// λ£¨νΈ λλ ν 리
"/"
// ν΄λ
"/Documents"
"/Pictures"
"/Documents/Work"
// νμΌ
"/Documents/file.txt"
"/Pictures/photo.jpg"
"/Documents/Work/report.pdf"
π Dependencies
π License
MIT License Β© Heebaek Choi