Estoy intentando crear carpetas en google drive utilizando su API. Pero me da el siguiente error
DetailedApiRequestError(status: 403, message: Request had insufficient authentication scopes.)
No se cual sea el problema ya que estoy tomando la informacion del usuario cuando inicia sesion final GoogleSignIn _googleSignIn =
GoogleSignIn(
scopes: [
drive.DriveApi.driveFileScope, // Solicita acceso a los archivos creados por la app.
drive.DriveApi.driveScope, // Solicita acceso completo a Google Drive.
],
);
Les paso el codigo completo
import 'package:flutter/material.dart';
import 'package:googleapis/drive/v3.dart' as drive;
import 'package:google_sign_in/google_sign_in.dart';
import 'package:http/http.dart' as http;
class TestFolder extends StatefulWidget {
@override
_TestFolderState createState() => _TestFolderState();
}
class _TestFolderState extends State<TestFolder> {
final GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
drive.DriveApi.driveFileScope,
drive.DriveApi.driveScope,
],
);
GoogleSignInAccount? _currentUser;
drive.DriveApi? _driveApi;
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
setState(() {
_currentUser = account;
});
if (_currentUser != null) {
_handleSignIn();
}
});
_googleSignIn.signInSilently();
}
Future<void> _handleSignIn() async {
final authHeaders = await _currentUser?.authHeaders;
if (authHeaders == null) return;
final authenticateClient = _GoogleAuthClient(authHeaders);
setState(() {
_driveApi = drive.DriveApi(authenticateClient);
});
}
Future<void> _createFolderinDrive() async {
if (_driveApi == null) {
print('Google Drive API isn't initializate');
return;
}
final folder = drive.File();
folder.name = "Folder [email protected]";
folder.mimeType = "application/vnd.google-apps.folder";
try {
final folderResponse = await _driveApi!.files.create(folder);
print('Folder Created Successfully in Drive: ${folderResponse.id}');
} catch (e) {
print('Error creating folder: $e');
}
}
Future<void> _signIn() async {
try {
await _googleSignIn.signIn();
} catch (error) {
print('Log in Error: $error');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Create Folder in Google Drive'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _currentUser != null ? _crearCarpetaEnDrive : _signIn,
child: Text(_currentUser != null ? 'Create Folder in Drive' : 'Google log in'),
),
],
),
),
);
}
}
class _GoogleAuthClient extends http.BaseClient {
final Map<String, String> _headers;
final http.Client _client = http.Client();
_GoogleAuthClient(this._headers);
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
return _client.send(request..headers.addAll(_headers));
}
}
Alguien me puede guiar con esto, ya que no he encontrado una solucion, he leido las API's, el funcionamiento y no encuentro nada, ya habilite la api en Google Cloud Console y tengo los OAuth y me sigue dando el mismo error, he checado los permisos y se que por la parte de Cloud Console no es, y de ahi en mas la informacion que he encontrado es inconclusa.