如何从 Google API 中获取用户的组织名称
How to obtain user's organization name from Google APIs
我们的应用使用 OpenID 连接对 Google 的用户进行身份验证,然后尝试使用 Google 的 API 获取用户组织的名称,因为它在 Open ID 连接 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();
虽然返回了用户,但组织的值为空,即使组织是在 Google Apps 管理面板中定义的。如何获得经过身份验证的 Google 用户的公司名称?
以下调用使用 Google 目录 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();
我们的应用使用 OpenID 连接对 Google 的用户进行身份验证,然后尝试使用 Google 的 API 获取用户组织的名称,因为它在 Open ID 连接 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();
虽然返回了用户,但组织的值为空,即使组织是在 Google Apps 管理面板中定义的。如何获得经过身份验证的 Google 用户的公司名称?
以下调用使用 Google 目录 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();