【问题标题】:Google App Engine App - authenticate to Google Admin Directory APIGoogle App Engine 应用程序 - 向 Google Admin Directory API 进行身份验证
【发布时间】: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 上运行的应用进行身份验证?

【问题讨论】:

标签: java google-app-engine oauth-2.0 google-cloud-platform google-admin-sdk


【解决方案1】:

我建议您查看 java quickstart sample 以获取 Google Admin SDK Directory API。

例如,您可以使用getCredentials 方法代替getClientCredential

/**
 * Creates an authorized Credential object.
 * @param HTTP_TRANSPORT The network HTTP Transport.
 * @return An authorized Credential object.
 * @throws IOException If the credentials.json file cannot be found.
 */
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = AdminSDKDirectoryQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多