google-app-engine – 在App Engine上使用OAuth2的IMAP

我正在尝试在App Engine上实施一项服务,该服务使用OAuth2,
Java 7和App Engine SDK 1.8.2与Gmail帐户进行交互.遇到的问题是通过使用
https://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCode提供的示例代码,安全提供程序似乎没有在节点上检测到,尽管在本地工作正常.之前链接提供的代码已被修改为由带有示例代码的servlet启动:

import java.io.IOException;
import java.security.Provider;
import java.security.Security;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.URLName;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.samples.oauth2.OAuth2SaslClientFactory;
import com.sun.mail.imap.IMAPSSLStore;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.smtp.SMTPTransport;


public class RunnerServlet extends HttpServlet
{


    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
    {

        String email = request.getParameter( "email");
        String oauthToken = request.getParameter( "oauthToken");

        initialize();

        try
        {
            IMAPStore imapStore = connectToImap("imap.gmail.com",
                    993,
                    email,
                    oauthToken,
                    true);

            System.out.println("Successfully authenticated to IMAP.\n");
            SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",
                    587,
                    email,
                    oauthToken,
                    true);
            System.out.println("Successfully authenticated to SMTP.");
        }
        catch( Exception e )
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw new RuntimeException( e );
        }
    }

    public static final class OAuth2Provider extends Provider {
        private static final long serialVersionUID = 1L;

        public OAuth2Provider() {

            super("Google OAuth2 Provider", 1.0,
                    "Provides the XOAUTH2 SASL Mechanism");

            put("SaslClientFactory.XOAUTH2",
                    "com.google.code.samples.oauth2.OAuth2SaslClientFactory");
        }
    }

    public static void initialize() {
        Security.addProvider(new OAuth2Provider());
    }

    public static IMAPStore connectToImap(String host, int port,
                                          String userEmail, String oauthToken, boolean debug)
            throws Exception {
        Properties props = new Properties();
        props.put("mail.imaps.sasl.enable", "true");
        props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
        props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
        Session session = Session.getInstance(props);
        session.setDebug(debug);

        session.getProperties().put("mail.imaps.sasl.enable", "true");
        session.getProperties().put("mail.imaps.sasl.mechanisms", "XOAUTH2");
        session.getProperties().put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);


        final URLName unusedUrlName = null;
        IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
        final String emptyPassword = "";
        store.connect(host, port, userEmail, emptyPassword);
        return store;
    }
    public static SMTPTransport connectToSmtp(String host, int port,
                                              String userEmail, String oauthToken, boolean debug)
            throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.sasl.enable", "true");
        props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
        props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
        Session session = Session.getInstance(props);
        session.setDebug(debug);

        final URLName unusedUrlName = null;
        SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
        // If the password is non-null, SMTP tries to do AUTH LOGIN.
        final String emptyPassword = null;
        transport.connect(host, port, userEmail, emptyPassword);

        return transport;
    }
}

IMAP调试跟踪是:

DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: enable SASL
DEBUG IMAPS: SASL mechanisms allowed: XOAUTH2
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from xxx.xxx.xxx.xxx ZZZZZZZZZ
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY 
A0 OK Thats all she wrote! ZZZZZZZZZ
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=som...@gmail.com, password=<non-null>
DEBUG IMAPS: SASL authentication command trace suppressed
DEBUG IMAPS: SASL Mechanisms:
DEBUG IMAPS:  XOAUTH2
DEBUG IMAPS: 
DEBUG IMAPS: No SASL support
DEBUG IMAPS: SASL authentication failed
DEBUG IMAPS: LOGIN command trace suppressed
DEBUG IMAPS: LOGIN command result: A1 NO Empty username or password. ZZZZZZZZZ
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from xxx.xxx.xxx.xxx YYYYYYYYYY
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2
A0 OK Thats all she wrote! YYY
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=som...@gmail.com, password=<non-null>
DEBUG IMAPS: SASL authentication command trace suppressed
DEBUG IMAPS: SASL Mechanisms:
DEBUG IMAPS:  XOAUTH2
DEBUG IMAPS: 
DEBUG IMAPS: No SASL support
DEBUG IMAPS: SASL authentication failed
DEBUG IMAPS: LOGIN command trace suppressed
DEBUG IMAPS: LOGIN command result: A1 NO Empty username or password. YYYYYYYYYY

java.lang.RuntimeException: javax.mail.AuthenticationFailedException: Empty username or password. YYYYYYYYYY

此问题仅发生在已部署的节点上.确保提供程序是正确的路径并安装并使用最新版本的App Engine SDK,以便于IMAP和SMTP套接字.已通过示例servlet和任务队列上的任务尝试触发.

感谢您的帮助.

最佳答案 我和你有同样的问题……

我一直在研究,我认为会话没有正确实例化.我附上了一张显示差异的图片.

在图像的顶部显示变量“props”的内容,它与AppEngine项目(左图)的项目Normal(右图)的值相同.
下图显示了Session变量的内容,其中包含变量Properties的内容.就像你在左边的例子中看到的那样,是null.但在正确的情况下有价值观.

这是图像:http://ricorrico.comoj.com/misc/img2.jpg

它确实是一个错误,有没有解决方法?

非常感谢你提前.

问候!

点赞