首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从Google API获取用户的组织名称

如何从Google API获取用户的组织名称
EN

Stack Overflow用户
提问于 2018-01-27 13:51:10
回答 1查看 176关注 0票数 1

我们的应用程序使用OpenID connect对谷歌用户进行身份验证,然后尝试使用谷歌的app获取用户组织的名称,因为它在Open ID Connect app对象中不可用。代码如下:

代码语言:javascript
运行
复制
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 Admin面板中定义的,组织的值也是空的。如何获得经过身份验证的Google用户的公司名称?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-31 18:58:35

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

代码语言:javascript
运行
复制
 directory.customers().get("my_customer").execute();

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

代码语言:javascript
运行
复制
https://www.googleapis.com/auth/admin.directory.customer.readonly

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

代码语言:javascript
运行
复制
            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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48473032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档