getSharedFileById method

  1. @override
Future<String> getSharedFileById({
  1. required String fileId,
  2. required String localPath,
})
override

Implementation

@override
Future<String> getSharedFileById({
  required String fileId,
  required String localPath,
}) 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) {
    throw Exception('Access token is null or empty');
  }

  final response = await http.get(
    Uri.parse(
        'https://graph.microsoft.com/v1.0/me/drive/items/$fileId/content'),
    headers: {
      'Authorization': 'Bearer $accessToken',
    },
  );

  if (response.statusCode != 200) {
    throw Exception(
        'Failed to download file by item ID: ${response.statusCode}, ${response.body}');
  }

  final file = File(localPath);
  await file.writeAsBytes(response.bodyBytes);
  return localPath;
}