The application is built via maven 3, where running has been performed on 
both dev app server and as a stand alone application both work locally. 
Where the versions of the libraries are:


   - javax.mail:mail:1.5.0-b01 ( although also tried versions 1.4.7 and 
   1.4.6 )
   - appengine sdk version 1.8.2
   - Java source code for connection can be found 
   at https://code.google.com/p/google-mail-oauth2-tools/source/checkout

The application has been triggered via a servlet as below:

    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;
        }
    }


Everything works fine locally regardless of how the code is triggered. 
Replicating the issue seen on the actual deployed App Engine instance can 
only be replicated by disabling the registration of the OAuth provider 
locally. Placement of debug messages within the OAuth2SaslClient and 
OAuth2SaslClientFactory indicate that the provider is not being hit at all 
on the node, although running locally these are being reached. Checked and 
all libraries and imports are on the whitelist for App Engine that relate 
to the Security Provider.

Thanks,
Glen

On Monday, 29 July 2013 16:48:20 UTC+10, Vinny P wrote:
>
> On Sun, Jul 28, 2013 at 6:07 PM, Glen Whitaker wrote:
>
>> I'm trying to implement a service on App Engine that interacts with an 
>> Gmail account using OAuth2, Java 7 and App Engine SDK 1.8.2. The problem 
>> being encountered is via the use of sample code provided by 
>> https://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCodethe 
>> Security provider does not appear to be detected on a node although 
>> works locally fine. 
>>
>  
>  
> When you run it locally, are you running it within the dev appserver or as 
> a stand-alone application? Did you build your application and add in the 
> libraries with Maven, or are you bundling them manually? Which libraries 
> are you adding (full names and version numbers preferred).
>   
>  
> -----------------
> -Vinny P
> Technology & Media Advisor
> Chicago, IL
>
> App Engine Code Samples: http://www.learntogoogleit.com
>   
>  
>  
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to