簡體   English   中英

Google App Engine應用-對Google Admin Directory API進行身份驗證

[英]Google App Engine App - authenticate to Google Admin Directory API

我正在嘗試從在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

這提供了指向有關將OAuth 2.0與Google App Engine應用程序的授權代碼流一起使用的指南的鏈接。

該指南提供了以下示例,說明如何創建GoogleAuthorizationCodeFlow ,但是沒有解釋什么getClientCredential()或我應該在該例程中做什么:

return new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
    getClientCredential(), Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(
    DATA_STORE_FACTORY).setAccessType("offline").build();

有關使用App Engine身份API的這一部分看起來很有希望,但是沒有說明如何將其與Google Admin Directory API客戶端庫一起使用:

https://cloud.google.com/appengine/docs/standard/java/appidentity/#asserting_identity_to_google_apis

我需要怎么做才能從在App Engine上運行的應用程序進行身份驗證?

建議您檢查Java快速入門示例以獲取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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM