4

OAuth2、Java 7、および App Engine SDK 1.8.2 を使用して Gmail アカウントとやり取りするサービスを App Engine に実装しようとしています。発生している問題は、https://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCodeによって提供されるサンプル コードの使用によるものです地元でいい。前のリンクで提供されたコードは、サンプル コードを使用してサーブレットによって開始されるように変更されています。

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

この問題は、デプロイされたノードでのみ発生します。プロバイダが正しいパスであり、インストールされ、IMAP および SMTP ソケットを容易にする最新バージョンの App Engine SDK を使用していることを確認しました。トリガーは、タスク キューのサンプル サーブレットとタスクを介して試行されました。

事前にご支援いただきありがとうございます。

4

2 に答える 2

1

私はあなたと同じ問題を抱えています...私は調査してきましたが、セッションが正しくインスタンス化されていないと思います。違いがわかる写真を添付し​​ました。

画像の上部には、変数「props」の内容が表示されます。これは、AppEngine プロジェクト (左の画像) の値とプロジェクト Normal (右の画像) の値は同じです。下の画像はセッション変数の内容を示しており、この中には変数のプロパティの内容が示されています。左のケースでわかるように、null です。しかし、正しい場合には値があります。

ここに画像があります:http://ricorrico.comoj.com/misc/img2.jpg

これは本当にバグなのですが、回避策はありますか?

事前にどうもありがとうございました。

よろしく!

于 2013-08-23T11:14:03.520 に答える