kotlin - 尽管用户列表 API 正常工作,但无法访问用户监视 API(返回状态 403)
问题描述
我使用具有范围 ( https://www.googleapis.com/auth/admin.directory.user.readonly ) 的服务帐户密钥来访问 Google Directory API 用户。
通过与以下相同的设置,我可以访问用户列表 API(链接)。
但是当尝试向用户观察 API(链接)发出请求时,状态 403 返回。
这 2 个 API 需要与我的 SA 密钥具有相同范围的 user.readonly。
我的域已经过验证并添加到 GCP Console 的域验证屏幕中。
val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
val jsonFactory = JacksonFactory()
val inputStream = HealthController::class.java.getResourceAsStream("/credentials.json")
?: throw FileNotFoundException("/credentials.json")
val credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory)
.toBuilder()
.setServiceAccountScopes(listOf(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY))
.setServiceAccountUser("admin@example.com")
.build()
service = Directory.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Some Name")
.build()
val channel = Channel()
channel.address = "https://example.com/webhook/v1/google/users"
channel.expiration = Instant.now().toEpochMilli() + 6 * 60 * 60 * 1000
channel.id = "webhook001"
channel.token = "abcxyz"
channel.type = "web_hook"
channel.payload = false
val result = service.users().watch(channel)
.setDomain("example.com")
.setViewType("domain_public")
.execute()
我将问题记录到 Google 问题跟踪器:https ://issuetracker.google.com/issues/171300784
解决方案
Google Directory API 不直接支持 Kotlin,它依赖于目标编译
先决条件
确保您已激活向服务帐户委派域范围的权限。
解决方法
作为 JAVA 中的一种方法(如果需要,您可以将此代码转换为 Kotlin)首先使用官方文档的watch 方法以及Channel 类,我将包依赖项放在注释中:
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// com.google.auth.oauth2.ServiceAccountCredentials
GoogleCredentials credentials = ServiceAccountCredentials.fromStream(new FileInputStream(SERVICE_ACCOUNT_CREDENTIALS_FILE_PATH))
.createScoped(SCOPES)
.createDelegated("impersonated@example.com");
// com.google.api.client.http
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
// com.google.api.services.admin.directory.Directory.Builder
Directory service = new Directory.Builder(httpTransport, jsonFactory, requestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
// com.google.api.services.admin.directory.model.Channel
Channel channel = new Channel();
channel.setAddress("https://example.com/webhook/v1/google/users");
channel.setExpiration(Instant.now().toEpochMilli() + 6 * 60 * 60 * 1000);
channel.setId("webhook001");
channel.setToken("abcxyz");
channel.setType("web_hook");
channel.setPayload(false);
System.out.println(service.users().watch(channel)
.setDomain("example.com")
.setViewType("domain_public")
.execute());
参考
推荐阅读
- regex - 正则表达式在单词第一次出现时停止
- visual-studio-code - 编辑器自定义:如何更改布尔关键字的颜色?
- nginx - 如何在集群边缘和客户端证书身份验证中使用 SSL 终止公开 GKE TCP 服务
- sql - 合并行连接值
- elasticsearch - 在dict中增加嵌套的json数组,然后将输出数组对象传递给elasticsearch
- python - 如何使用python获取mongodb中键的值
- asp.net - 使用 asp.net-core 的 React SPA 在部署之间不更新(缓存)
- django - 在另一个应用程序中使用应用程序模型的对象字段作为表单 ChoiceField
- python-3.x - 使用熊猫获取数据框的第二列
- android - 如何在android中使用Volley发送对象的JSON数组