Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Non-nullable return
  • Loading branch information
stuartmorgan-g committed Jun 24, 2025
commit 6933e906a57af6d7bd60f0e0cfa03fb20a7ea26e
26 changes: 13 additions & 13 deletions packages/extension_google_sign_in_as_googleapis_auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ That object can then be used to create instances of `googleapis` API clients:

<?code-excerpt "example/lib/main.dart (CreateAPIClient)"?>
```dart
// Retrieve an [auth.AuthClient] from the current [GoogleSignIn] instance.
final auth.AuthClient? client = await _googleSignIn.authenticatedClient();

assert(client != null, 'Authenticated client missing!');

// Prepare a People Service authenticated client.
final PeopleServiceApi peopleApi = PeopleServiceApi(client!);
// Retrieve a list of the `names` of my `connections`
final ListConnectionsResponse response =
await peopleApi.people.connections.list(
'people/me',
personFields: 'names',
);
import 'package:googleapis_auth/googleapis_auth.dart' as auth show AuthClient;
// ···
// Retrieve an [auth.AuthClient] from a GoogleSignInClientAuthorization.
final auth.AuthClient client = authorization.authClient(scopes: scopes);

// Prepare a People Service authenticated client.
final PeopleServiceApi peopleApi = PeopleServiceApi(client);
// Retrieve a list of connected contacts' names.
final ListConnectionsResponse response =
await peopleApi.people.connections.list(
'people/me',
personFields: 'names',
);
```

## Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sig
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/people/v1.dart';
// #docregion Import
// #docregion CreateAPIClient
import 'package:googleapis_auth/googleapis_auth.dart' as auth show AuthClient;
// #enddocregion Import
// #enddocregion CreateAPIClient

/// The scopes used by this example.
const List<String> scopes = <String>[PeopleServiceApi.contactsReadonlyScope];
Expand Down Expand Up @@ -79,7 +79,7 @@ class SignInDemoState extends State<SignInDemo> {
});

if (authorization != null) {
unawaited(_handleGetContact());
unawaited(_handleGetContact(authorization));
}
}

Expand All @@ -93,19 +93,18 @@ class SignInDemoState extends State<SignInDemo> {
.authorizeScopes(<String>[PeopleServiceApi.contactsReadonlyScope]));
}

Future<void> _handleGetContact() async {
Future<void> _handleGetContact(
GoogleSignInClientAuthorization authorization) async {
setState(() {
_contactText = 'Loading contact info...';
});

// #docregion CreateAPIClient
// Retrieve an [auth.AuthClient] from a GoogleSignInClientAuthorization.
final auth.AuthClient? client = _authorization?.authClient(scopes: scopes);

assert(client != null, 'Authenticated client missing!');
final auth.AuthClient client = authorization.authClient(scopes: scopes);

// Prepare a People Service authenticated client.
final PeopleServiceApi peopleApi = PeopleServiceApi(client!);
final PeopleServiceApi peopleApi = PeopleServiceApi(client);
// Retrieve a list of connected contacts' names.
final ListConnectionsResponse response =
await peopleApi.people.connections.list(
Expand Down Expand Up @@ -154,6 +153,7 @@ class SignInDemoState extends State<SignInDemo> {
future: _signInInitialized,
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
final GoogleSignInAccount? user = _currentUser;
final GoogleSignInClientAuthorization? authorization = _authorization;
final List<Widget> children;
if (snapshot.hasError) {
children = <Widget>[
Expand All @@ -170,10 +170,10 @@ class SignInDemoState extends State<SignInDemo> {
subtitle: Text(user.email),
),
const Text('Signed in successfully.'),
if (_authorization != null) ...<Widget>[
if (authorization != null) ...<Widget>[
Text(_contactText),
ElevatedButton(
onPressed: _handleGetContact,
onPressed: () => _handleGetContact(authorization),
child: const Text('REFRESH'),
),
] else ...<Widget>[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension GoogleApisGoogleSignInAuth on GoogleSignInClientAuthorization {
/// The [scopes] passed here should be the same as the scopes used to request
/// the authorization. Passing scopes here that have not been authorized will
/// likely result in API errors when using the client.
gapis.AuthClient? authClient({
gapis.AuthClient authClient({
required List<String> scopes,
}) {
final gapis.AccessCredentials credentials = gapis.AccessCredentials(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
FakeGoogleSignInClientAuthorization();
final gapis.AuthClient client = signInAuth.authClient(
scopes: scopes,
)!;
);
expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN));
expect(client.credentials.scopes, equals(scopes));
});
Expand Down