Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Google API in flutter?

Tags:

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 ?

like image 532
PrimeNexes Avatar asked Jan 27 '18 16:01

PrimeNexes


People also ask

How do I add a Google API key to Flutter?

Adding Google Maps to Flutter app (Android) First, open your Flutter project and navigate to the file at this location: android/app/src/main/AndroidManifest. xml . Replace the value "YOUR KEY HERE" with an API key you created. Then, add the location permission.

Is Google Map API free?

You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).

What is the use of Google API?

Google Cloud APIs are programmatic interfaces to Google Cloud Platform services. They are a key part of Google Cloud Platform, allowing you to easily add the power of everything from computing to networking to storage to machine-learning-based data analysis to your applications.


1 Answers

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));  } 
like image 163
Günter Zöchbauer Avatar answered Sep 30 '22 15:09

Günter Zöchbauer