title: Gmail REST API : 400 Bad Request + Failed Precondition
body: <p>I'm trying to send mails based on Gmail REST API using google java api services. I have configured through Google Develover Console an application client and downloaded p12 and json files.</p>
  
  <p>I have used this sample programs, <a href="https://developers.google.com/gmail/api/guides/sending#sending_messages" rel="noreferrer">https://developers.google.com/gmail/api/guides/sending#sending_messages</a>...</p>
  
  <p>This sample works,  but this is based on GoogleAuthorizationCodeFlow. I want just to work from server-to-server, invoking directly, not opening a browser to get an access token... and I got it (the access token) but finally i receive a Bad Request.... Why??? I don't receive more info just than <strong><em>"Bad Request" and "Precondition Failed"</em></strong></p>
  
  <p>Based on this i follow the next steps:</p>
  
  <ol>
  <li><p><strong>First Step</strong>: Create a GoogleCredential object based on my client account mail and p12 generated file:</p>
  
  <pre><code>GoogleCredential  credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport())
                                      .setJsonFactory(new JacksonFactory())
                                      .setServiceAccountId(serviceAccountUserEmail)
                                      .setServiceAccountScopes(scopes)
                                      .setServiceAccountPrivateKeyFromP12File(
                                                  new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))                             
                                                          .build();
  </code></pre></li>
  </ol>
  
  <p>Here i have to point that i had many issues for ussing  ClientID instead of ClientMail. It must use @developer.gserviceaccount.com account instead of .apps.googleusercontent.com . If u don't send this params ok , u get an "INVALID GRANT" error. This is explained here: <a href="https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization" rel="noreferrer">https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization</a></p>
  
  <ol start="2">
  <li><p><strong>Second Step</strong> : Create the Gmail Service based on the credentials:</p>
  
  <pre><code>Gmail gmailService = new Gmail.Builder(httpTransport,
                                                jsonFactory,
                                                credential)
                                              .setApplicationName(APP_NAME)
                                                  .build();
  </code></pre></li>
  <li><p><strong>Third Step</strong> Create a Google raw message from a MimmeMessage:</p>
  
  <pre><code>private static Message _createMessageWithEmail(final MimeMessage email) throws MessagingException, IOException {
  
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  email.writeTo(bytes);
  String encodedEmail =       Base64.encodeBase64URLSafeString(bytes.toByteArray());      
  Message message = new Message();    
  message.setRaw(encodedEmail);
  return message;
  </code></pre>
  
  <p>}</p></li>
  <li><p><strong>Fourth Step</strong> Invoke the service:</p>
  
  <pre><code>    Message message = _createMessageWithEmail(email);
  message = service.users()
  .messages()
  .send(userId,message)
  .execute();
  </code></pre></li>
  <li><strong>Fifth Step</strong> : Get Result after execution...Here i receive the exception:</li>
  </ol>
  
  <p>Exception in thread "main" </p>
  
  <pre><code>com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
  {
    "code" : 400,
    "errors" : [ {
      "domain" : "global",
      "message" : "Bad Request",
      "reason" : "failedPrecondition"
    } ],
    "message" : "Bad Request"
  }
      at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
  </code></pre>
  
  <p><strong>Any idea  what is wrong or which is the Precondition failed??</strong></p>