Skip to content

[google_sign_in and googleapis] Not able to access Google Drive API after updating Google Sign in to V7 #173407

@sagarehs

Description

@sagarehs

Steps to reproduce

Hello Everyone, Not able to access Google Drive API after updating google_sign_in: ^7.1.1

Google Sing-in is working fine for me using firebase auth and google-service.json but there is one issue regarding to access google drive API in flutter app

when I login using v7 not able to read or write google drive file

API Key is restricted for Android using package and sha1

For you kind information File read and write is working fine with v6

Expected results

I want to read and write file with Google Drive using googleapis flutter package

Actual results

Got Error in Google Drive API

DetailedApiRequestError(status: 403, message: Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.)

Code sample

Code sample
  GoogleSignIn _googleSignIn = GoogleSignIn(scopes: [
    'https://www.googleapis.com/auth/drive.appdata',
    'https://www.googleapis.com/auth/drive.file'
  ]);

 @override
  void initState() {
    // TODO: implement initState
    super.initState();

    WidgetsBinding.instance.addObserver(this);
    _initializeGoogleSignIn();
  }

Future<void> _initializeGoogleSignIn() async {
    try {
      await _googleSignIn.initialize(serverClientId: '263300586130-0fjelbeit8a27e7pb3maauh1t9dcdrvc.apps.googleusercontent.com');
      _isGoogleSignInInitialized = true;
    } catch (e) {
      print('Failed to initialize Google Sign-In: $e');
    }
  }

  /// Always check Google sign in initialization before use
  Future<void> _ensureGoogleSignInInitialized() async {
    if (!_isGoogleSignInInitialized) {
      await _initializeGoogleSignIn();
    }
  }
  Future<GoogleSignInAccount> signInWithGoogle() async {
    await _ensureGoogleSignInInitialized();

    try {
      // authenticate() throws exceptions instead of returning null
      final GoogleSignInAccount account = await _googleSignIn.authenticate(
        scopeHint: ['email','https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/drive.file'],  // Specify required scopes
      );
      return account;
    } on GoogleSignInException catch (e) {
      print('Google Sign In error: code: ${e.code.name} description:${e.description} details:${e.details}');
      rethrow;
    } catch (error) {
      print('Unexpected Google Sign-In error: $error');
      rethrow;
    }
  }

//OnClick of Download File
      _currentUser = await signInWithGoogle();
      var client = GoogleHttpClient(await _currentUser!.authorizationClient.authorizationHeaders(['https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/drive.file']));
var _drive = v3.DriveApi(client);
      v3.FileList parentlist = new v3.FileList();
      v3.FileList csvFile = new v3.FileList();

class GoogleHttpClient extends IOClient {
  Map<String, String>? _headers;

  GoogleHttpClient(this._headers) : super();

  @override
  Future<IOStreamedResponse> send(http.BaseRequest request) {
    // TODO: implement send
    return super.send(request..headers.addAll(_headers!));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: invalidIssue is closed as not valid

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions