【发布时间】:2019-05-14 06:51:33
【问题描述】:
我正在尝试从在 App Engine 上运行的 Java 应用程序中访问 Google Admin Directory API 以创建新的 Google 群组。我正在使用以下依赖项:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.25.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>1.25.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-admin-directory</artifactId>
<version>directory_v1-rev105-1.25.0</version>
</dependency>
然后我尝试创建一个 Google 群组,如下所示:
final List<String> SCOPES = Collections.singletonList(DirectoryScopes.ADMIN_DIRECTORY_GROUP);
AppIdentityCredential appCredential = new AppIdentityCredential(SCOPES);
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
Directory directory = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, getClientCredential())
.setApplicationName("Test")
.build();
com.google.api.services.admin.directory.model.Group group = new Group();
group.setEmail("test@test.com");
group.setName("test_group");
group.setDescription("test_group_desc");
Group googleGroup = directory.groups().insert(group).execute();
我收到403 错误,我认为我需要以不同的方式进行身份验证。我查看了以下在 Google App Engine 上使用适用于 Java 的 Google API 客户端库的指南:
https://developers.google.com/api-client-library/java/google-api-java-client/app-engine
这提供了指向using OAuth 2.0 with the authorization code flow for Google App Engine applications 上的指南的链接
该指南给出了如何创建GoogleAuthorizationCodeFlow 的以下示例,但是没有说明getClientCredential() 是什么或我应该在该例程中做什么:
return new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
getClientCredential(), Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(
DATA_STORE_FACTORY).setAccessType("offline").build();
关于使用 App Engine Identity API 的这一部分看起来很有希望,但是没有说明如何将其与 Google Admin Directory API 客户端库一起使用:
https://cloud.google.com/appengine/docs/standard/java/appidentity/#asserting_identity_to_google_apis
我需要做什么才能从 App Engine 上运行的应用进行身份验证?
【问题讨论】:
-
您实施了 G Suite 委派吗? developers.google.com/admin-sdk/reports/v1/guides/delegation您的代码没有实现模拟,这是要求之一。
标签: java google-app-engine oauth-2.0 google-cloud-platform google-admin-sdk