How to Utilize Google API In Flutter?


The Google Cloud Platform services are accessible through programmatic interfaces on the Google Cloud Platform. They are a crucial component of the Google Cloud Platform, enabling you to effortlessly add the power of everything from processing to networking to storage to machine learning-based data analysis to your apps.

The available package google sign in 5.4.2 can be used.
Consequently, after signing up using the package google sign in, obtaining auth headers from it:

import 'package:google_sign_in/google_sign_in.dart'
    show GoogleSignIn, GoogleSignInAccount;

import 'package:googleapis/people/v1.dart'
    show ListConnectionsResponse, PeopleApi;

googleApi() async {
  final _googleSignIn = new GoogleSignIn();

  await _googleSignIn.signIn();

  final authHeaders = _googleSignIn.currentUser.authHeaders;

  final httpClient = GoogleHttpClient(authHeaders); 

  data = await PeopleApi(httpClient).people.connections.list(
      personField: 'names,addresses',
      token: nextToken,
      pageSize: 50,

This is a unique IOClient implementation that adds the authentication headers to every request automatically. Therefore, passing a specific HTTP client to be used instead of the default is supported by the Google APIs calls.

import 'package:http/io_client.dart';
import 'package:http/http.dart';

class GoogleClient extends IOClient {
  Map<String, String> _headers;

  GoogleClient(this._headers) : super();

  Future<StreamedResponse> send(BaseRequest request) =>

  Future<Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));
Dependencies Using pubspec.yaml:
  google_sign_in: any
  googleapis: any

How it operates: 

import 'package:googleapis/drive/v3.dart' as drive;
import 'package:google_sign_in/google_sign_in.dart' as signIn;

Step 1: Log in to Google Drive and request access permission (scope)

final googleSignIn = signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.DriveScope]);
final sign.GoogleSignInAccount account = await googleSignIn.signIn();

Step 2: Create an AuthenticateClient

class AuthenticateClient extends http.BaseClient {
  final Map<String, String> headers;

  final http.Client client;

  AuthenticateClient(this.headers, this.client);

  Future<http.StreamedResponse> send(http.BaseRequest request) {
    return client.send(request..headers.addAll(headers));

Thus, this uses the BaseClient with additional authentication headers as proposed by HTTP.

Step 3: Use the HTTP client you created in steps 1 and 2 to access the Google Drive API.

final baseClient = new Client();
final authenticateClient = AuthenticateClient(authHeader, baseClient);
final driveApi = drive.DriveApi(authenticateClient);


Thank you for joining us on a flutter journey! I hope you were successful in finding what you were looking for.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories