1. 权限获取
官方文档:https://developers.google.com/identity/protocols/oauth2
gmail遵循auth2协议,需要在页面授权拿到code(授权码) ,服务端就可以保存code到数据库,通过接口请求获取access_token和refresh_token(参考文章:使用HTTP获取 OAuth 2.0 access tokens(Google)_菜鸟的学习笔记-CSDN博客_chrome浏览器怎么查看oauth获取的token)
refresh_token 很重要,access_token每3600秒失效,需要通过refresh_token重新请求获取,所以做个定时任务,每五十分钟刷新一下access_token即可。
注:refresh_token的有效期基本是永久,除非用户改密码或者权限收回
所以,权限的获取,就是要搞清楚code,access_token 和 refresh_token的区别
2. 邮件同步
参考官方文档:https://developers.google.com/gmail/api/guides/sync
2.1 全量同步
2.2 增量同步
全量同步后获取的historyId入库,下次同步从historyId开始往后继续同步
(1) historyId 404问题解决
参考文档:https://developers.google.com/gmail/api/reference/rest/v1/users.history/list
historyId会失效,所以需要进行一次全量的同步(通过messageId去重),重新获取有效的 historyId作增量同步
(2) 内容乱码
遵循Base64 RFC 4648 标准. 需要替换-
为+
, _
为 /
(3)只同步指定日期的邮件
参考文档:https://support.google.com/mail/answer/7190