In my recent project I need to access the email from gmail inbox. I implemented the functionality but there is an issue when I implemented this, the issue is that user has to enable the less secure sign-in technology if they want that app should read the inbox mails. That sounds odd, then I found the solution for this on oracle site faq where they have suggested for Oauth2.
To do the authorisation with Oauth2 you need to go through the following steps.
1. Obtain OAuth 2.0 credentials from the Google API Console.
2. Obtain an access token from the Google Authorization Server.
To obtain the access token you first required to get an oauth code, which you can obtain as:
1 2 3 4 5 6 7 8 9 10 11 |
def getAuthCode(HttpServletResponse response) { String client_id = "925793710742-2vg1hd7eglvu3sgja8nj52j2vgt7al6a.apps.googleusercontent.com"; String redirect_uri = "http://localhost:8080/messagefetcher/messageRead/oauthcallback" try { def authUrl = new GoogleAuthorizationCodeRequestUrl(client_id, redirect_uri, ["https://www.googleapis.com/auth/gmail.readonly https://mail.google.com/"]) String url = authUrl.setAccessType("online").setApprovalPrompt("force").build() response.sendRedirect(url); } catch (Exception ex) { log.error("Exception: " + ex) } } |
You will get oauth code on the url which you have provided in the google API console in my case redirect url is:
http://localhost:8080/messagefetcher/messageRead/oauthcallback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def oauthcallback() { String authorizationCode = request.getParameter("code") String client_id = "925793710742-2vg1hd7eglvu3sgja8nj52j2vgt7al6a.apps.googleusercontent.com"; String client_secret = "*******************"; String redirect_uri = "http://localhost:8080/messagefetcher/messageRead/oauthcallback" HttpTransport netTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleTokenResponse token = new GoogleAuthorizationCodeTokenRequest(netTransport, jsonFactory, client_id, client_secret, authorizationCode, redirect_uri).execute(); System.out.println("Valid access token " + token.getAccessToken()); GoogleCredential cd = new GoogleCredential().setAccessToken(token .getAccessToken()); return cd?.accessToken } |
Now you will be able to connect with gmail store without enabling the less secure sign-in technology as user logs in with their Google account. After logging in, the user is asked whether they are willing to grant the permissions that your application is requesting. This process is called user consent.
and you ready to connect with gmail with auth access tokenstore as:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Properties props = new Properties(); props.put("mail.imap.ssl.enable", "true"); // required for Gmail props.put("mail.imap.auth.mechanisms", "XOAUTH2"); Session session = Session.getInstance(props); session.setDebug(true); Store store = null; try { store = session.getStore("imap"); store.connect("imap.gmail.com", "myemail@gmail.com", accessToken); } catch (MessagingException e) { log.error("Messaging exception: "); e.printStackTrace(); } return store; |
Here I am using Javamail version 1.5.6
Recent Comments