【问题标题】:How can I get the data of the domain users using a service account?如何使用服务帐户获取域用户的数据?
【发布时间】:2020-12-15 05:59:20
【问题描述】:

我正在开发一个使用人员 API 目录 APi 的公司应用程序,但是,我总是需要从特定用户处自动检查,因为我无法使用服务帐户接收数据。如何使用服务帐号获取域用户的数据?

【问题讨论】:

  • 你好@MaximBezmen!不幸的是,我不明白你关于研究它的问题。当您说“我总是需要从特定用户那里自动检查,因为我无法使用服务帐户接收数据”时,我不了解手头的操作,请您澄清一下吗?
  • 我想在访问api时使用服务账号登录获取开放域配置文件。但是,我无法使用服务帐户请求此信息。您需要使用真实用户的数据登录。
  • 感谢您的澄清。为了更好地帮助您,请分享您正在使用的代码,以便我们大家看看。
  • 没有代码,因为我拒绝了这个解决方案。我将它用于真正的用户。用户使用他的数据登录 Google,我收到一个令牌并随后使用它。一般来说,在谷歌的恐惧中,新手开发者的文档非常困难和清晰,示例用于桌面应用程序(((
  • 很高兴您找到了解决此问题的方法。为了获得更好的文档,您能否描述您在答案中使用的解决方案?

标签: google-directory-api google-people-api


【解决方案1】:

我使用此代码连接 google API。

public class DirectoryService {
@Value("${service.account.email}")
private String SERVICE_ACCOUNT_EMAIL;
@Value("${service.account.pk.file.path}")
private String SERVICE_ACCOUNT_PKCS12_FILE_PATH;

public Directory getDirectoryService(String userEmail){
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    Directory service = null;
    try {
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(Collections.singleton(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY))
                .setServiceAccountUser(userEmail)
                .setServiceAccountPrivateKeyFromP12File(
                        new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
                .build();
        service = new Directory.Builder(httpTransport, jsonFactory, null)
                .setHttpRequestInitializer(credential).build();
    } catch (GeneralSecurityException e){
        log.error("Responding \"GeneralSecurityException in Google API.\" in method getDirectoryService().");
        throw new BadRequestException("GeneralSecurityException in Google API.");
    }catch (IOException e){
        log.error("Responding \"Not connection with Google API.\" in method getDirectoryService().");
        throw new BadRequestException("Not connection with Google API.");
    }

    return service;
}

}

        Directory directory = directoryService.getDirectoryService(email);
        Users response = directory.users().list()
                .setDomain("domain.com")
                .setViewType("domain_public")
                .setMaxResults(500)
                .execute();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多