有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在不需要OAuth授权的情况下代表组织用户提供密码

根据目前已弃用的Google Apps Provisioning API,我们的组织能够代表我们的用户提供密码,只需授权为超级管理员用户,如下所示:

 User userService = new UserService(GoogleAppsPasswordSyncConnector.class.getSimpleName());
 AuthTokenFactory authTokenFactory = AuthTokenFactoryThreadLocal.getAuthTokenFactory(adminUsername, adminPassword);
 userService.setAuthTokenFactory(authTokenFactory);

然而,根据Directory API: Authorize Requests的说法,使用新的目录API时,要求“对目录API的所有请求必须由经过身份验证的用户授权。”这是基于OAuth2.0来授权请求的

我们如何代表我们的组织用户设置密码,而不要求他们授权请求

请注意,在某些情况下,我们可能手头有他们当前的Google密码,因此我们可能会以编程方式代表他们进行授权。但是,对于新的Google帐户,我们不知道设置的密码

请注意,我们利用“GADS”(谷歌应用程序目录同步)来提供新的谷歌帐户


共 (2) 个答案

  1. # 1 楼答案

    无论您如何看待它,都必须在链接时使用OAuth进行身份验证。然而,如果你不想做举重,你可能想看看其他一些工具。如果你在Windows中工作。Net/C#看看我的项目gShell,它最基本的级别是wrapper for the authentication in .Net,但也提供了PowerShell cmdlet,您可以编写脚本

    在PowerShell中,完成一次性身份验证后,可以使用Set-GAUser以编程方式更新用户密码,如下所示:

    Set-GAUser MyUser -NewPassword ThisIsTheNewPassword
    

    或者你可以让它为你生成一个,如果你愿意的话

    如果您计划使用Python或想使用命令行,请查看Google Apps Managerby jay0lee,这也是一个很好的资源

    至于新帐户,您应该仍然能够使用GADS创建新帐户和密码。如果您不使用它作为密码,您应该能够使用上述方法之一来绑定现有代码

    最后,如果您没有像我在这里假设的那样使用Windows,那么我提到的两个项目都是开源的,您可以浏览代码以获取如何进行身份验证的示例,尽管您已经找到的Google资源可能更直截了当一些

  2. # 2 楼答案

    在google developers console(https://console.developers.google.com/project)API和身份验证凭据中创建服务帐户类型OAuth client id,并在API项目中启用Admin SDK,然后将API作用域“https://www.googleapis.com/auth/admin.directory.user”添加到https Admin的作用域中。谷歌。com/AdminHome在安全“管理API客户端访问”中,我可以使用以下代码设置密码。请注意,用于建立上述配置的帐户对相关谷歌电子邮件具有管理权限

            HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
    
            Credential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setServiceAccountId(clientEmail)
                    .setServiceAccountUser(adminUser)
                    .setServiceAccountPrivateKeyFromP12File(new File(privateKeyStoreLocation))
                    .setServiceAccountScopes(scopes).build();
    
            Directory directory = new Directory.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName(applicationName)
                    .build();
    
            Directory.Users.Get get = directory.users().get(userEmail);
            User user = get.execute();
            user.setPassword(newPassword);
            directory.users().patch(userEmail, user).execute();