【问题标题】:How to obtain user's organization name from Google APIs如何从 Google API 获取用户的组织名称
【发布时间】:2018-01-27 05:51:10
【问题描述】:

我们的应用使用 OpenID Connect 通过 Google 对用户进行身份验证,然后尝试使用 Google 的 API 获取用户组织的名称,因为它在 Open ID Connect UserInfo 对象中不可用。代码如下:

JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleCredential credential = getCredentials(jsonFactory, httpTransport, connection.getDomainAdminEmail());
Directory directory = new Directory.Builder(httpTransport, jsonFactory, null).setApplicationName(APP_NAME).setHttpRequestInitializer(setHttpTimeout(credential)).build();
User user = directory.users().get(id).execute();
Object organizations = user.getOrganizations();

虽然返回了用户,但 organizations 的值为 null,即使该组织是在 Google Apps 管理控制台中定义的。如何获取经过身份验证的 Google 用户的公司名称?

【问题讨论】:

    标签: google-directory-api


    【解决方案1】:

    以下调用使用 Google Directory API 检索客户信息:

     directory.customers().get("my_customer").execute();
    

    注意:使用 my_customer 将检索当前客户。此调用至少需要以下范围:

    https://www.googleapis.com/auth/admin.directory.customer.readonly
    

    似乎无法使用服务凭据获取客户信息。它需要是客户端 ID 凭据。登录的用户必须同意上述范围。

                GoogleCredential credential = getCredentials(jsonFactory, httpTransport, connection.getDomainAdminEmail(),true);
    
                Directory directory = new Directory.Builder(httpTransport, jsonFactory, null)
                    .setApplicationName(APP_NAME)
                    .setHttpRequestInitializer(setHttpTimeout(credential))
                    .build();
                    Customer customer = directory.customers().get("my_customer").execute();
                    CustomerPostalAddress postAddress = customer.getPostalAddress();
    
                googleCustomer = new GoogleCustomer.Builder()
                    .setPhoneNumber(customer.getPhoneNumber())
                    .setLanguage(customer.getLanguage())
                    .setCompany(postAddress.getOrganizationName())
                    .setAddress1(postAddress.getAddressLine1())
                    .setAddress2(postAddress.getAddressLine2())
                    .setAddress3(postAddress.getAddressLine3())
                    .setAddressContactName(postAddress.getContactName())
                    .setAddressCountryCode(postAddress.getCountryCode())
                    .setAddressLocality(postAddress.getLocality())
                    .setAddressPostalCode(postAddress.getPostalCode())
                    .setAddressRegion(postAddress.getRegion())
                    .build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2021-12-06
      相关资源
      最近更新 更多