【问题标题】:Using Dart googleapis_auth (0.2.2) to authorize Admin SDK Directory API in Google Apps domains with service account使用 Dart googleapis_auth (0.2.2) 在具有服务帐户的 Google Apps 域中授权 Admin SDK Directory API
【发布时间】:2015-04-01 19:10:04
【问题描述】:

我想通过服务帐户授权访问 Google Apps 域中的 Admin SDK Directory API。据我了解,它需要JWT claim with a sub field,但我在 pub 包 googleapis_auth (0.2.2) 中找不到它。

如果丢失:

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

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

【问题讨论】:

标签: dart google-oauth google-admin-sdk


【解决方案1】:

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

代表管理员用户 admin@domain.com 列出在具有服务帐户的 Google Apps 域中使用 Admin SDK Directory 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));
    });
  });
}

【讨论】:

  • 我现在使用的是 googleapis_auth 0.2.3,它工作正常。我相信您的代码示例应该是“impersonatedUser:'admin@domain.com'”非常感谢!
【解决方案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();
});

【讨论】:

  • Google Apps 要求声明集中有一个子字段,其中包含管理员的电子邮件地址。我找不到将其包含在 googleapis_auth 中的方法。也许我错过了什么?谢谢
  • 我认为 Google 只需要 issaudscopeexpiat,其中 iss 设置为“client_email”。
  • 我承认我在这里深陷困境,但您如何解释本页“附加声明”下所说的内容:developers.google.com/accounts/docs/OAuth2ServiceAccount
  • 常规服务帐户不需要sub 声明。您需要它来模拟用户。
  • 此页面上还有一条注释:developers.google.com/admin-sdk/directory/v1/guides/delegation "注意:只有有权访问 Admin API 的用户才能访问 Admin SDK Directory API,因此您的服务帐户需要模拟这些用户之一访问管理 SDK 目录 API。”谢谢
猜你喜欢
  • 1970-01-01
  • 2020-01-06
  • 2014-10-12
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多