title: Sending an email from a node.js client with a service account
body: <p>So i have the following code:</p>
  
  <pre><code>var Path = require('path');
  var google = require('googleapis');
  var gmail = google.gmail('v1');
  var key = require(Path.join(__dirname, 'googleConfig.json'));
  var oAuthClient = new google.auth.JWT(
      key.client_email,
      null,
      key.private_key,
      ['https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/gmail.send'],
      null
  );
  
  var message = new Buffer(
      "Content-Type: text/plain; charset=\"UTF-8\"\n" +
      "MIME-Version: 1.0\n" +
      "Content-Transfer-Encoding: 7bit\n" +
      "To: &lt;some email&gt;\n" +
      "From: &lt;some email&gt;\n" +
      "Subject: something something lol\n\n" +
      "heyheyhey"
  ).toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
  
  gmail.users.messages.send({
      auth: oAuthClient,
      userId: '&lt;my own google email?&gt;',
      resource: {
          raw: message
      }
  }, function(err, resp) {
      console.log('err', err);
      console.log('resp', resp);
  });
  </code></pre>
  
  <p>And i use the same generated key to my google calendar API, so the service account is active and working. </p>
  
  <p>I get the following error:</p>
  
  <pre><code>{ [Error: Bad Request]
    code: 400,
    errors: 
     [ { domain: 'global',
         reason: 'failedPrecondition',
         message: 'Bad Request' } ] }
  </code></pre>
  
  <p>Is it correct to assume that the userId is my own google email account?
  i have allso tried to use the <code>key.client_email</code> and <code>client_id</code>  instead, which didn't work. </p>
  
  <p>Im really quite lost :)  any help, or even just tips on how to debug it would be awesome.   </p>
  
  <p>Btw, i did activate the gmail API on console.developers</p>