29

I want to use Google Cloud Natural Language in my Flutter app,I got Google API package This works for flutter and theGoogle API_AUTH dependence is working for 0.2.1. How do I implement them ?

2
  • 2
    I really doubt googleapis_auth works on flutter since it depends on http. Commented Jan 27, 2018 at 18:11
  • 1
    html I mean sorry Commented Jan 28, 2018 at 3:36

4 Answers 4

37

This worked for me:

Logging in using package google_sign_in and then get 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;

useGoogleApi() async {
  final _googleSignIn = new GoogleSignIn(
    scopes: [
      'email',
      'https://www.googleapis.com/auth/contacts.readonly',
    ],
  );

  await _googleSignIn.signIn();

  final authHeaders = _googleSignIn.currentUser.authHeaders;

  // custom IOClient from below
  final httpClient = GoogleHttpClient(authHeaders); 

  data = await PeopleApi(httpClient).people.connections.list(
      'people/me',
      personFields: 'names,addresses',
      pageToken: nextPageToken,
      pageSize: 100,
  );
}

This is a custom IOClient implementation that automatically adds the auth headers to each request. The googleapis call support passing a custom HTTP client to be used instead of the default (see above)

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

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

  GoogleHttpClient(this._headers) : super();

  @override
  Future<StreamedResponse> send(BaseRequest request) =>
      super.send(request..headers.addAll(_headers));

  @override
  Future<Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));

}
Sign up to request clarification or add additional context in comments.

18 Comments

Can I use this GoogleHttpClient to send email using gmailAPI and/or to send invites using FireBase Invites?
I haven't used these APIs, but if Google provides APIs for these tasks, you should be able to do it with GiigleHttpClient.
Was able to get it working for now using stackoverflow.com/questions/50079538/… . Will try to use your approach in future.
I invented it in my answer above ;) It's just the name I used for my custom subclass of IOClient
Thanks for this answer. Now with extension_google_sign_in_as_googleapis_auth package, you can use ready-to-use IOClient provided by this package instead of your own GoogleHttpClient implementation. It's a little change, but I posted as another answer.
|
14

I can't add comments yet, so I'll just post it as an answer.

I kept trying to make a GoogleHttpClient as per the top answer, but on the import, it says "The library 'package:http/http.dart' doesn't export a member with the shown name 'IOClient'."

I found the answer here https://pub.dartlang.org/packages/http#-changelog-tab-, which says you should import IOClient separately as such: import 'package:http/io_client.dart';

I thought this might help out others who are new to flutter and its implementation of Google APIs.

1 Comment

Thank you @Kim btw how to Add this "@Kim Palao" ? In my case it's just a simple string
4

The accepted answer is most likely written towards an older version of the SDK I just couldn't get it to work. This is what works for me as of now.

As an example, the following allow us to access files in Google Drive which is part of googleapis.

Dependencies

pubspec.yaml:

dependencies:
  google_sign_in: any
  googleapis: any

(I just put any here as a example, but you should specific the version(s) for you actual app.)

How it works

Necessary imports:

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

Step 1, sign in the user and ask for access permission (scope) to google drive:

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

Step 2, build a 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));
  }
}

As suggested in http, this is using the BaseClient with extra authentication headers (being composable).

Step 3, create a authenticated http client with from step 1 and 2 and access google drive API.

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

Checkout How to Use the Google Drive API With Flutter Apps for details.

1 Comment

Can I ask what's the difference between this method and using the authenticatedClient function from the googleapis_auth package to get an authenticated client?
3

Update to the accepted answer

Along with google_sign_in and googleapis packages, now you can use extension_google_sign_in_as_googleapis_auth package (provided by flutter.dev) to get configured http client. So the accepted answer can be simplified to below. No implementation of GoogleHttpClient is necessary.

import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/people/v1.dart';

useGoogleApi() async {
  final _googleSignIn = new GoogleSignIn(
    scopes: [
      'email',
      PeopleServiceApi.contactsReadonlyScope,
    ],
  );

  await _googleSignIn.signIn();

  // custom IOClient
  final httpClient = await _googleSignIn.authenticatedClient();

  data = await PeopleApi(httpClient!).people.connections.list(
      'people/me',
      personFields: 'names,addresses',
      pageToken: nextPageToken,
      pageSize: 100,
  );
}

2 Comments

Where can I found "nextPageToken" ?
@Tananga, I've not tested with this specific piece of code because that part of code is just copied from the accepted answer. But, according to the People API documentation, nextPageToken is provided in the response of this method, and can be omitted or null since it is optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.