title: How do you download an attachment with Gmail API with nodejs?
body: <p>Im trying to make a node app on my local computer that does this:</p>
  
  <p>1) Looks at my gmail inbox.</p>
  
  <p>2) If an email has a certain label and an attachment, download it.</p>
  
  <p>Im on step 2.</p>
  
  <p>The part Im confused about is the part about 'parts'.</p>
  
  <p><a href="https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get" rel="nofollow noreferrer">https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get</a></p>
  
  <p>Googles sample code:</p>
  
  <pre><code>function getAttachments(userId, message, callback) {
    var parts = message.payload.parts;
    for (var i = 0; i &lt; parts.length; i++) {
      var part = parts[i];
      if (part.filename &amp;&amp; part.filename.length &gt; 0) {
        var attachId = part.body.attachmentId;
        var request = gapi.client.gmail.users.messages.attachments.get({
          'id': attachId,
          'messageId': message.id,
          'userId': userId
        });
        request.execute(function(attachment) { // &lt;--- that attachment param
          callback(part.filename, part.mimeType, attachment);
        });
      }
    }
  }
  </code></pre>
  
  <p>It seems that 'attachment' contains the attachment data.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>const gmail = google.gmail({version: 'v1', auth});
  
  gmail.users.messages.list({
    userId: 'me',
    'q':'subject:my-test has:nouserlabels has:attachment'
  }, (err, res) =&gt; {
    if (err) return console.log('1 The API returned an error: ' + err);
  
    const msgs = res.data.messages;
  
    if(typeof msgs !== 'undefined'){
      console.log('Messages:');
      msgs.forEach( msg =&gt; {
  
        console.log(`- ${msg.id}`);
  
        gmail.users.messages.get({
          userId: 'me',
          id: msg.id,
          format:'full'
        },(err, res) =&gt; {
          if (err) return console.log('2 The API returned an error: ' + err);
  
          // Wheres the data????? //
  
          console.log('A')
          console.log(res.data.payload.parts)
  
          console.log('B')
          console.log(res.data.payload.parts[1])
  
          console.log('C')
          console.log(res.data.payload.parts[1].body)
        })
      });
    } else {
      console.log('no messages found')
    }
  
  
  });
  </code></pre>