1

Der Versuch, E-Mails über die Google Mail-API in App Engine zu senden. Zuvor habe ich einen Dienstkontoschlüssel auf der Anmeldeseite erstellt. Er generiert eine .P12-Datei, die sich im Parameter setServiceAccountPrivateKeyFromP12File befindet. Es hat einen ID-Schlüssel, der mit dem Konto [email protected] auf der Seite des Dienstkontos verknüpft ist. Der Code:E-Mails über die Google Mail-API senden Google App Engine

/* Application name. */ 
    private static final String APPLICATION_NAME = "appnamefromappengine"; 

    String emailAddress = "[email protected]"; 
    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 
    try { 
     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 

     Set<String> scopes = new HashSet<String>(); 
     scopes.add(GmailScopes.GMAIL_SEND); 
     scopes.add(GmailScopes.GMAIL_COMPOSE); 
     scopes.add(GmailScopes.MAIL_GOOGLE_COM); 
     scopes.add("https://www.googleapis.com/auth/admin.directory.user"); 

     GoogleCredential credential = new GoogleCredential.Builder() 
      .setTransport(httpTransport) 
      .setJsonFactory(JSON_FACTORY) 
      .setServiceAccountId(emailAddress) 
      .setServiceAccountPrivateKeyFromP12File(new File("/home/myuser/Test/src/main/webapp/resources/**somename**cd30e7118ad5.p12")) 
      .setServiceAccountScopes(scopes) 
      .setServiceAccountUser("[email protected]") 
      .build(); 
     Gmail gmail = new Gmail 
      .Builder(httpTransport, JSON_FACTORY, credential) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 

     Properties props = new Properties(); 
     Session session = Session.getDefaultInstance(props, null); 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("rec[email protected]")); 
     message.setSubject("Test Mail"); 
     message.setText("Test Mail"); 

     Message msg = createMessageWithEmail(message); //createMessageWithEmail function from Gmail API 
     msg = gmail.users().messages().send(emailAddress, msg).execute(); 

     System.out.println("Mail was sent. Message id: " + msg.getId()); 

    } catch (GeneralSecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (MessagingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

Es gibt mir diese Fehlermeldung:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request { "code" : 400,
"errors" : [ { "domain" : "global", "message" : "Bad Request", "reason" : "failedPrecondition" } ],
"message" : "Bad Request" }

Ich bin nicht sicher, welche Parameter ich hier falsch in Code habe die Einrichtung oder in Google Cloud-Konsole. Was kann ich noch versuchen?

+2

prüfen diese Frage SO [Google Mail REST API: 400 Bad Request + fehlgeschlagen Voraussetzung] (http://stackoverflow.com/questions/29327846/gmail-rest- api-400-bad-request-failed-precondition) wenn es dir helfen kann :) – KENdi

+0

War der Link von @KENDI hilfreich? Konnten Sie die fehlerhafte Bedingung eingrenzen oder beheben? – Nicholas

+1

Es war @ Nicholas. Das Problem war, dass ich keine domänenweite Delegate-Berechtigung für das Dienstkonto in der Admin-Konsole konfiguriert habe. So oder so funktioniert der gesamte Code, wenn jemand eine E-Mail mit der Google Mail-API senden möchte. Die Frage könnte ergänzt werden Antwort, wie E-Mail-API autorisierte Absender in den Code zu verwenden. Sobald jemand eine E-Mail in diesem Bereich registriert hat, erfahren Sie, wie Sie auf diese E-Mail verweisen können. Danke für alles. –

Antwort

2

Das Problem war, dass ich Delegate domänenweite Befugnis nicht zu dem Dienstkonto in Admin-Konsole konfigurieren.

  1. Go to your Google Apps domain’s Admin console.

  2. Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.

  3. Select Show more and then Advanced settings from the list of options.

  4. Select Manage API client access in the Authentication section.

  5. In the Client Name field enter the service account's Client ID. You can find your service account's client ID in the Service accounts section of the Developers Console's Permissions page.

  6. In the One or More API Scopes field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive , https://www.googleapis.com/auth/calendar .

  7. Click Authorize.

die gesamte Dokumentation:
https://developers.google.com/identity/protocols/OAuth2ServiceAccount