JavaMail-在轮询电子邮件时如何更改gmail oauth访问令牌?

问题描述

我们有一个通用库/应用程序,可使用JavaMail处理不同的邮件服务。我需要为此应用编写一种插件,该插件使用OAuth访问令牌连接到gmail。在插件中,我必须重写以下方法

public Store initStore();
public void connectStore(Store store,String host,String port,String userEmail,String password);
public void openInBox(Store store);
public Message[] getMessages(Folder inBox);
public void disConnectStore(Store store);

因此,在initStore()中,我正在使用以下代码创建商店:

    Properties props = new Properties();
    props.put("mail.imaps.sasl.enable","true");
    props.put("mail.imaps.sasl.mechanisms","XOAUTH2");
    props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken",oauthToken);
    Session session = Session.getInstance(props);
    session.setDebug(debug);
    
    store = session.getStore("imaps");
    return store;

在connectStore(...)中,我有以下代码

    final String emptyPassword = "";
    store.connect(host,port,userEmail,emptyPassword);

类似地,我在其他方法添加代码-访问收件箱,返回邮件等...

但是在disConnectStore(Store store)方法中,我们需要关闭商店,因此我添加了:

store.close()

主库按以下顺序调用方法

  1. 它仅调用一次initStore()
  2. 每隔15分钟,它将继续调用connectStore(),openInBox(),getMessages()和disConnectStore()。

一切正常,但我们知道,Google OAuth访问令牌会在1小时后过期。因此,在1小时后,在我的connectStore()中,出现以下异常:

javax.mail.AuthenticationFailedException: [AUTHENTICATIONFailed] Invalid credentials (Failure)

所以,我的问题是-此时如何更改存储对象中的访问令牌?这可能吗?或者,能否请您分享一些我可以放入connectStore()中的代码,如果该令牌过期了,我可以在其中更改访问令牌?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)