使用 Dart googleapis_auth (0.2.2) 授权 Admin SDK Directory API in Google Apps domains with service account

Using Dart googleapis_auth (0.2.2) to authorize Admin SDK Directory API in Google Apps domains with service account

我想授权使用服务帐户访问 Google 应用程序域中的 Admin SDK 目录 API。据我了解,它需要 JWT claim with a sub field and I can't find that in the pub package googleapis_auth (0.2.2).

如果丢失:

有解决办法吗? 它会包含在未来的版本中吗?

目前我正在安装一个经过用户同意授权的应用程序(管理员帐户),但这有点乏味...

我相信 https://github.com/dart-lang/googleapis_auth#autonomous-application--service-account 中描述的支持。在 0.2.3 版本中,ServiceAccountCredentials 的构造函数现在具有可选的命名参数 impersonatedUser,可用于将用户设置为模拟。

import "package:http/http.dart" as http;
import "package:googleapis_auth/auth_io.dart";

var accountCredentials = new ServiceAccountCredentials.fromJson({
  "private_key_id": "<please fill in>",
  "private_key": "<please fill in>",
  "client_email": "<please fill in>@developer.gserviceaccount.com",
  "client_id": "<please fill in>.apps.googleusercontent.com",
  "type": "service_account"
}, impersonatedUser: 'user@domain.com');
var scopes = [...];

...

var client = new http.Client();
obtainAccessCredentialsViaServiceAccount(accountCredentials, scopes, client)
    .then((AccessCredentials credentials) {
  // Access credentials are available in [credentials].
  // ...
  client.close();
});

googleapis_auth 的 0.2.3 版本中,ServiceAccountCredentials 的构造函数具有可选的命名参数 impersonatedUser,可用于将用户设置为模拟。

代表管理员用户 admin@[=20 列出 Google 具有服务帐户的应用程序域中使用 Admin SDK 目录 API 的所有用户的代码=] 看起来像这样:

import 'package:googleapis/admin/directory_v1.dart';
import 'package:googleapis/drive/v2.dart';
import 'package:googleapis_auth/auth_io.dart';

final credentials = new ServiceAccountCredentials.fromJson({
  "private_key_id": "<please fill in>",
  "private_key": "<please fill in>",
  "client_email": "<please fill in>",
  "client_id": "<please fill in>",
  "type": "service_account"
}, user: 'admin@domain.com');

const SCOPES = const [AdminApi.AdminDirectoryGroupScope,
                      AdminApi.AdminDirectoryUserScope];
void main() {
  clientViaServiceAccount(credentials, SCOPES).then((http_client) {
    var admin = new AdminApi(http_client);
    admin.users.list(domain: 'domain.com').then((Users users) {
      users.users.forEach((user) => print(user.name.fullName));
    });
  });
}