How to get Google account photo in Flutter

Issue #187

If you use Flutter, then you can access it via people.googleapis.com endpoint, code uses google_sign_in library

1
2
3
4
5
6
7
8
9
10
11
12
13
import 'package:google_sign_in/google_sign_in.dart';

Future<String> getPhotoUrl(GoogleSignInAccount account, String userId) async {
// final authentication = await account.authentication;
final url = 'https://people.googleapis.com/v1/people/${userId}?personFields=photos';
final response = await http.get(
url,
headers: await account.authHeaders
);

final data = json.decode(response.body);
return data['photos'].first['url'];
}

You will get something like

1
2
3
4
5
6
{
resourceName: people/998812322529259873423,
etag: %EgQBAzcabcQBAgUH,
photos: [{metadata: {primary: true, source: {type: PROFILE, id: 107721622529987673423}},
url: https://lh3.googleusercontent.com/a-/abcdefmB2p1VWxLsNT9WSV0yqwuwo6o2Ba21sh_ra7CnrZ=s100}]
}

where url is an accessible image url.

Comments