如何在Flutter中使用Google API? [英] How to use Google API in flutter?
问题描述
我想在Flutter应用中使用Google Cloud Natural Language,我得到了 Google API程序包
这适用于抖动, Google API_AUTH 依赖性适用于0.2.1。
如何实现它们?
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 ?
推荐答案
这对我有用:
使用软件包 google_sign_in
登录,然后从中获取身份验证标头:
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,
);
}
这是自定义 IOClient
实现,可将auth标头自动添加到每个请求。 googleapis调用支持传递自定义HTTP客户端(而不是默认值)(请参见上文)
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));
}
这篇关于如何在Flutter中使用Google API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!