google_photos 1.0.0 copy "google_photos: ^1.0.0" to clipboard
google_photos: ^1.0.0 copied to clipboard

outdated

Autogenerated Google Photos API client

Google Photos Autogenerated API client #

Google Photos does not yet have a first-party Dart SDK, so this is an unofficial API client autogenerated from Google Photo's API discovery doc

Caveat lector: Although this library is completely autogenerated using discoveryapis_generator, it is also not tested. I'm using it in my own projects and have not run into any problems, but please open an issue if you run into any problems.

Getting Started #

This API client is designed to be used with Dart's standard HTTP library. However, most of the endpoints require authentication headers to function properly.

Flutter #

You can use the google_sign_in package to get these headers, and extend the HTTP BaseClient to inject the auth headers on behalf of the API client:

import 'package:http/http.dart';

main() async {
  final GoogleSignIn googleSignIn = GoogleSignIn(scopes: <String>[
    'profile',
    'https://www.googleapis.com/auth/photoslibrary',
    'https://www.googleapis.com/auth/photoslibrary.sharing'
  ]);
  final user = await googleSignIn.signInSilently();
  final client = AuthenticatedClient(
    Client(),
    () => user.authHeaders,
    user.clearAuthCache,
  );
  return PhotoslibraryApi(client);
}

class AuthenticatedClient extends BaseClient {
  final Client baseClient;
  final Future<Map<String, String>> Function() getAuthHeaders;
  final Future<void> Function() invalidHeadersCallback;

  AuthenticatedClient(
    this.baseClient,
    this.getAuthHeaders,
    this.invalidHeadersCallback,
  );

  Future<StreamedResponse> send(final BaseRequest request) async {
    var authHeaders = await getAuthHeaders();
    request.headers.addAll(authHeaders);
    var response = await baseClient.send(request);
    if (response.statusCode == 401) {
      // Headers are expired, or perhaps user has been logged out.
      // GoogleSignIn expects clients to inform it of invalid cached headers.
      await invalidHeadersCallback();
    }
    return response;
  }

  @override
  void close() {
    super.close();
    baseClient.close();
  }
}

In future, perhaps the Dart or Flutter teams will provide better integration of HTTP clients with the google_sign_in package.

Everything else #

Use the existing googleapis_auth library to get an authenticated HTTP client.

1
likes
0
pub points
26%
popularity

Publisher

unverified uploader

Autogenerated Google Photos API client

Homepage

License

unknown (LICENSE)

Dependencies

_discoveryapis_commons, http

More

Packages that depend on google_photos