Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android对接Exchange Web Services (EWS)服务 #222

Open
soapgu opened this issue Aug 18, 2023 · 0 comments
Open

Android对接Exchange Web Services (EWS)服务 #222

soapgu opened this issue Aug 18, 2023 · 0 comments
Labels

Comments

@soapgu
Copy link
Owner

soapgu commented Aug 18, 2023

  • 引子

从前2010年以前Exchange服务是“如日中天”一般存在,几乎所有使用windows系统作为服务的企业都要搭载一套exchange邮件系统。以前也碰到过对接Exchange的项目需求,但是在后来Exchange的出镜率以及越来越少了。这次正好有项目碰到对Exchange
的需求。

  • Exchange对接接口

Exchange的对外接口命名为Exchange Web Services (EWS)
接口地址为https://{server ip}/EWS/exchange.asmx
感觉WebService微软一开始是走在前面的,对接web service感觉非常方便,直接Visaul Studio 一键生成就搞定了,直接生成操作类和Model。
不过这次是要Android对接,一下子就有点棘手。

好在有个[ews-java-api](https://github.com/OfficeDev/ews-java-api)
库可以支持。一看作者是Office Dev,还算是官方认证。看star也马马虎虎。就是这个更新时间……
图片
基本属于“古董+年久失修”项目,有一定风险

  • 对接过程

只是先试试再说了。

  1. 引用下库
dependencies {
    compile 'com.microsoft.ews-java-api:ews-java-api:2.0'
}
  1. 设置 服务器地址,账号邮箱和密码
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials("emailAddress", "password");
service.setCredentials(credentials);
  1. 绑定收件箱,并收信息
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
 public void listFirstTenItems() {
	ItemView view = new ItemView(10);
	FindItemsResults<Item> findResults = service.findItems(folder.getId(), view);
        
        //MOOOOOOST IMPORTANT: load messages' properties before
        service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
	
        for (Item item : findResults.getItems()) {
		// Do something with the item as shown
		System.out.println("id==========" + item.getId());
		System.out.println("sub==========" + item.getSubject());
	}
}
  • 是不是一气呵成,哪有这么顺利,直接就报错,是底层的http组件找不到。一头雾水。

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/protocol/HttpClientContext;

原因就是这个组件是在太老了,当时的安卓版本估计是还是20左右。我们现在用的是AndroidX,底层已经重写了一遍了。似乎走了一条死路。

  • 天无绝人之路

好在开源届还是有活雷锋

  • ews-android-api
    帮我们把Android X的适配已经做好了
    根据Readme,需要自己手工打包引用,还需要改下项目配置,但还是比较方便的
  1. 打包jar
  2. 增加另外三个官方依赖
  3. 修改Proguard rules
    完美运行
  • 获取日程信息&RxJava改造

public Single<List<AppointmentEntity>> findAppointmentsRx(Date startDate, Date endDate){
        return Single.<List<AppointmentEntity>>create( emitter -> {
            try{
                List<AppointmentEntity> list = findAppointments(startDate, endDate);
                emitter.onSuccess(list);
            }catch (Exception ex){
                emitter.onError(ex);
            }
        }).subscribeOn(Schedulers.io());
    }

    private List<AppointmentEntity> findAppointments(Date startDate, Date endDate) throws Exception {
        CalendarFolder folder=CalendarFolder.bind(service, WellKnownFolderName.Calendar);
        FindItemsResults<Appointment> findResults = folder.findAppointments(new CalendarView(startDate, endDate));
        List<Appointment> list = findResults.getItems();
        List<AppointmentEntity> retValue = new ArrayList<>();
        for ( Appointment appointment: list
             ) {
            appointment.load(PropertySet.FirstClassProperties);
            AppointmentEntity entity = new AppointmentEntity( appointment.getStart(),
                    appointment.getEnd(),appointment.getSubject(),appointment.getOrganizer().getName());
            retValue.add( entity );
        }
        return retValue;
    }
  • 关于https认证以及证书问题

由于https知识的基础薄弱,还是困扰很久的。
主要问题在与如果web service的https的证书不是用公网签发而是自签名生成的话,https的认证就过不了。

这里有两条路可以走

  1. 从客户端程序这里重写https的认证,把所有的认证都手工关掉
  2. 就是把根证书导入到安卓系统

理论上两条路都走得通。所有选择了更安全方便的线路2。
root过的安卓系统导入根证书还是方便的。
线路1理论上可行,就是代价风险高一点

关于CA认证相关

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant