如何使用discoveryapis_generator为我的Google Cloud Endpoints API生成Dart客户端库?

时间:2021-12-15 15:26:18

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.

我有一个端点API,我正在访问使用discoveryapis_generator生成的Dart客户端库。除了生成的库似乎不反映我的API的身份验证要求之外,一切都很好。

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

是否只需要以某种方式创建一个经过身份验证的http对象,以便传递给我的应用程序的BrowserClient()构造函数,如下所示?

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?

是否建议使用googleapis_auth包创建经过身份验证的http对象的方法,如此处所述?我是在正确的轨道上吗?

1 个解决方案

#1


2  

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...).

身份验证不是API本身的一部分。它实际上是http客户端,它将为用户身份验证发送正确的http标头。假设您使用标准的Google身份验证机制,则可以像使用标准Google API(云端硬盘等)一样使用https://pub.dartlang.org/packages/googleapis_auth包。

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)

您将不得不创建一个clientId(谷歌控制台)并使用BrowserOAuth2Flow来获取AuthClient(扩展http.client),然后再执行新的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

我有一个(非常古老的)项目,我覆盖google auth的标准行为,以允许在身份验证期间指定userId(从未真正找到该文档,但它有效),使用一个简单的示例,使用PlusApi获取用户名但它可以以类似的方式为你自己的api工作。也许这可以帮助https://github.com/alextekartik/tekartik_googleapis_auth.dart

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

我认为在调用createImplicitBrowserFlow时至少需要电子邮件范围

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

还有一些使用谷歌api的示例可以提供帮助:https://github.com/dart-lang/googleapis_examples

#1


2  

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...).

身份验证不是API本身的一部分。它实际上是http客户端,它将为用户身份验证发送正确的http标头。假设您使用标准的Google身份验证机制,则可以像使用标准Google API(云端硬盘等)一样使用https://pub.dartlang.org/packages/googleapis_auth包。

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)

您将不得不创建一个clientId(谷歌控制台)并使用BrowserOAuth2Flow来获取AuthClient(扩展http.client),然后再执行新的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

我有一个(非常古老的)项目,我覆盖google auth的标准行为,以允许在身份验证期间指定userId(从未真正找到该文档,但它有效),使用一个简单的示例,使用PlusApi获取用户名但它可以以类似的方式为你自己的api工作。也许这可以帮助https://github.com/alextekartik/tekartik_googleapis_auth.dart

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

我认为在调用createImplicitBrowserFlow时至少需要电子邮件范围

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

还有一些使用谷歌api的示例可以提供帮助:https://github.com/dart-lang/googleapis_examples