generateSharableLink method
Implementation
@override
Future<Uri?> generateSharableLink(String path) async {
if (!_isAuthenticated) {
throw Exception('Not authenticated');
}
final accessToken = await DefaultTokenManager(
tokenEndpoint: OneDrive.tokenEndpoint,
clientID: client.clientID,
redirectURL: client.redirectURL,
scope: client.scopes,
).getAccessToken();
if (accessToken == null || accessToken.isEmpty) {
debugPrint("OneDriveProvider: No access token available.");
return null;
}
final encodedPath =
Uri.encodeComponent(path.startsWith('/') ? path.substring(1) : path);
final driveItemPath = "/me/drive/root:/$encodedPath:/createLink";
try {
final response = await http.post(
Uri.parse("https://graph.microsoft.com/v1.0$driveItemPath"),
headers: {
'Authorization': 'Bearer $accessToken',
'Content-Type': 'application/json',
},
body: jsonEncode({
"type": "view", // or "edit" if you want an editable link
"scope": "anonymous"
}),
);
if (response.statusCode != 200) {
debugPrint(
"OneDriveProvider: Failed to create shareable link: ${response.body}");
return null;
}
final json = jsonDecode(response.body);
final link = json['link']?['webUrl'];
if (link == null) {
debugPrint("OneDriveProvider: No shareable link returned.");
return null;
}
// Append original path metadata as a query parameter
final shareableUri = Uri.parse(link).replace(
queryParameters: {
...Uri.parse(link).queryParameters,
'originalPath': path,
},
);
return shareableUri;
} catch (e) {
debugPrint("OneDriveProvider: Error creating shareable link: $e");
return null;
}
}