Tom Russell Tom Russell - 2 months ago 15
Dart Question

How do I generate a Dart client library for my Google Cloud Endpoints API using discoveryapis_generator?

I have an endpoints API that I'm accessing with a Dart client library generated with

discoveryapis_generator
. All is well and good except that the generated library doesn't appear to reflect the authentication requirements of my API.

Is it only necessary to somehow create an authenticated
http
object to pass to my application's
BrowserClient()
constructor in the following line?

my_api = new MyApi(new BrowserClient());


Is the recommended method for creating the authenticated
http
object to use the
googleapis_auth
package as described here? Am I on the right track?

Answer

The authentication is not part of the API itself. It is actually the http client that will send the proper http header for user authentication. Assuming you use the standard google auth mechanism, you can use the package https://pub.dartlang.org/packages/googleapis_auth as you would for a standard Google API (Drive, etc...).

You will have to create a clientId (google console) and use BrowserOAuth2Flow to get an AuthClient (that extends http.client) and from then do new MyApi(authClient)

I have a (quite old) project where I override the standard behavior of google auth to allow specifying a userId (never really found the doc on that but it works) during authentication with a simple example that use the PlusApi to get the user name but it could work in a similar way for your own api. Maybe that could help https://github.com/alextekartik/tekartik_googleapis_auth.dart

I think you need at least the email scope when calling createImplicitBrowserFlow

There are also samples for using google apis that could help: https://github.com/dart-lang/googleapis_examples