package com.testproject.util; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.json.JsonFactory; import com.google.api.client.util.store.FileDataStoreFactory; import com.google.api.services.gmail.GmailScopes; import com.google.api.services.gmail.model.*; import com.google.api.services.gmail.Gmail; import com.google.api.services.sheets.v4.SheetsScopes; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.List; public class Quickstart { /** Application name. */ private static final String APPLICATION_NAME = "Gmail API Java Quickstart"; /** Directory to store user credentials for this application. */ private static final java.io.File DATA_STORE_DIR = new java.io.File( System.getProperty("user.home"), ".credentials/2/sheets.googleapis.com-java-quickstart.json"); /** Global instance of the {@link FileDataStoreFactory}. */ private static FileDataStoreFactory DATA_STORE_FACTORY; /** Global instance of the JSON factory. */ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); /** Global instance of the HTTP transport. */ private static HttpTransport HTTP_TRANSPORT; /** Global instance of the scopes required by this quickstart. * * If modifying these scopes, delete your previously saved credentials * at ~/.credentials/gmail-java-quickstart */ private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS, SheetsScopes.DRIVE); static { try { HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); } catch (Throwable t) { t.printStackTrace(); System.exit(1); } } /** * Creates an authorized Credential object. * @return an authorized Credential object. * @throws IOException */ public static Credential authorize() throws IOException { // Load client secrets. InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json"); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory(DATA_STORE_FACTORY) .setAccessType("offline") .build(); Credential credential = null; try { credential = new AuthorizationCodeInstalledApp( flow, new LocalServerReceiver()).authorize("user"); } catch (Exception e) { e.printStackTrace(); } System.out.println( "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); return credential; } /** * Build and return an authorized Gmail client service. * @return an authorized Gmail client service * @throws IOException */ public static Gmail getGmailService() throws IOException { Credential credential = authorize(); return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME) .build(); } public static void main(String[] args) throws IOException { // Build a new authorized API client service. Gmail service = getGmailService(); // Print the labels in the user's account. String user = "me"; ListLabelsResponse listResponse = service.users().labels().list(user).execute(); List<Label> labels = listResponse.getLabels(); if (labels.size() == 0) { System.out.println("No labels found."); } else { System.out.println("Labels:"); for (Label label : labels) { System.out.printf("- %s\n", label.getName()); } } } } ---------- package com.testproject.rest; import com.google.api.services.gmail.Gmail; import com.testproject.util.GoogleAPI; import com.testproject.util.Quickstart; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import java.io.IOException; @Path("/gmail") public class MailSenderGoogleAPI { private static String to = "wsevolodmail@gmail.com"; private static String from = "wsevolodtest@gmail.com"; private static String subject = "Hello!"; private static String text = "bla bla bla"; @GET @Path("/send") public static Response sendEmail() throws MessagingException, IOException{ Gmail gmail = new Quickstart().getGmailService(); MimeMessage message = GoogleAPI.createEmail(to, from, subject, text); GoogleAPI.sendMessage(gmail, "me", message); return Response.status(200).entity("OK").build(); } } ---------- package com.testproject.util; import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64; import com.google.api.services.gmail.Gmail; import com.google.api.services.gmail.model.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Properties; public class GoogleAPI { public static void sendMessage(Gmail service, String userId, MimeMessage email) throws MessagingException, IOException { Message message = createMessageWithEmail(email); message = service.users().messages().send(userId, message).execute(); System.out.println("Message id: " + message.getId()); System.out.println(message.toPrettyString()); } private static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); email.writeTo(baos); String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray()); Message message = new Message(); message.setRaw(encodedEmail); return message; } public static MimeMessage createEmail(String to, String from, String subject, String bodyText) throws MessagingException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); InternetAddress tAddress = new InternetAddress(to); InternetAddress fAddress = new InternetAddress(from); email.setFrom(new InternetAddress(from)); email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to)); email.setSubject(subject); email.setText(bodyText); return email; } } ---------- - private static final List <String> SCOPES = Arrays.asList (GmailScopes.GMAIL_LABELS, GmailScopes.MAIL_GOOGLE_COM, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.GMAIL_METADATA); - Wsevolod
- Edit the question and add the full text of the error to it. - Sergey Gornostaev
|