Google Admin SDK:目录用户:如何使用Java客户端获取用户电话号码? [英] Google Admin SDK: Directory Users: How to get User phone numbers using Java client?

查看:80
本文介绍了Google Admin SDK:目录用户:如何使用Java客户端获取用户电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用服务帐户进行身份验证,并且可以在我公司的Google目录中列出用户.

但是我想列出用户的电话号码.

我使用了 java快速入门示例代码,有用.但是,它仅打印用户的主要电子邮件地址.

我想使用User.getPhones()方法获取每个用户的电话号码列表/数组,但是Java API返回对象",请参见解决方案

我想出了以下方法来转换Admin SDK中<String,Object>对象的ArrayMapsArrayMaps的通用List. 它需要以下导入:

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.ArrayMap;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

以下是执行解析的方法:

public <T extends GenericJson> List<T> parse(Object genericItem, Class<T> clazz){
     List<T> result = new ArrayList<T>();
     if (genericItem != null){
         try {
             Constructor<T> constructor = clazz.getConstructor(new Class[0]);    // find the default constructor of the input class type
             List<ArrayMap<String,Object>> objects = (List<ArrayMap<String,Object>>)genericItem;
             for (ArrayMap<String,Object> object: objects){
                T id = constructor.newInstance(); // call the default constructor
                for (int i = 0; i < object.size(); i++)
                    id.put(object.getKey(i), object.getValue(i));
                result.add(id);
            }
        } catch (NoSuchMethodException ex) {
               Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return result;
}

这是这样的:

List<UserPhone> phones = parser.parse(user.getPhones(), UserPhone.class);
List<UserExternalId> externalIds = parser.parse(user.getExternalIds(),    UserExternalId.class);
List<UserOrganization> organizations = parser.parse(user.getOrganizations(), UserOrganization.class);
List<UserEmail> userEmails = parser.parse(user.getEmails(), UserEmail.class); 

完成此操作后,您可以更新电话,externalIds ...,然后执行user.setPhones(phones),保存时User对象可以很好地处理新对象.

我认为您今天无法让API自动为您返回解析的结果.

I successfully authenticate using a Service Account and can list the users in my company's Google Directory.

However I want to list Users' telephone numbers.

I used the java quickstart example code and it works. However it is only printing the User's primary email address.

I wanted to use the User.getPhones() method to get the list/array of phone numbers for each user but the Java API returns "Object" see the Google java API

I know that the real result of the Google request is JSON and a User has an Array of Phones which have a "type" and a "value" eg work: num (Admin SDK User Representation)

I know that the google Java client is using a Google specific Jackson2 implementation,

com.google.api.client.json.jackson2.JacksonFactory

How can I influence it to produce UserPhone objects and not just java.lang.Object?

Cheers Karl

解决方案

I have come up with the following method that translate the generic List of ArrayMaps of <String,Object> objects in the Admin SDK. It requires the following imports:

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.ArrayMap;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Here's the method that does the parsing:

public <T extends GenericJson> List<T> parse(Object genericItem, Class<T> clazz){
     List<T> result = new ArrayList<T>();
     if (genericItem != null){
         try {
             Constructor<T> constructor = clazz.getConstructor(new Class[0]);    // find the default constructor of the input class type
             List<ArrayMap<String,Object>> objects = (List<ArrayMap<String,Object>>)genericItem;
             for (ArrayMap<String,Object> object: objects){
                T id = constructor.newInstance(); // call the default constructor
                for (int i = 0; i < object.size(); i++)
                    id.put(object.getKey(i), object.getValue(i));
                result.add(id);
            }
        } catch (NoSuchMethodException ex) {
               Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return result;
}

This works like this:

List<UserPhone> phones = parser.parse(user.getPhones(), UserPhone.class);
List<UserExternalId> externalIds = parser.parse(user.getExternalIds(),    UserExternalId.class);
List<UserOrganization> organizations = parser.parse(user.getOrganizations(), UserOrganization.class);
List<UserEmail> userEmails = parser.parse(user.getEmails(), UserEmail.class); 

Once you've done this you can update the phones, externalIds,... and then do user.setPhones(phones) and the User object will handle the new objects fine when saving.

I don't think you can have the API return the parsed result automatically for you today.

这篇关于Google Admin SDK:目录用户:如何使用Java客户端获取用户电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆