Failed sending mail through google api

假如想象 提交于 2019-12-08 06:40:16

问题


I've been trying to send emails using Google's Gmail API and I kept getting the following error:

The API returned an error: Error: 'raw' RFC822 payload message string or uploading message via /upload/* URL required


I did the setup using the starter code Google gave for NodeJS (documentation).

const google = require('googleapis');
const googleAuth = require('google-auth-library');
const Base64 = require('js-base64').Base64;

// ...

// create the email string
const emailLines = [];

emailLines.push("From: \"My Name\" <MY_EMAIL@gmail.com>");
emailLines.push("To: YOUR_EMAIL@uw.edu");
emailLines.push('Content-type: text/html;charset=iso-8859-1');
emailLines.push('MIME-Version: 1.0');
emailLines.push("Subject: New future subject here");
emailLines.push("");
emailLines.push("And the body text goes here");
emailLines.push("<b>And the bold text goes here</b>");
const email =email_lines.join("\r\n").trim(); 

// ...

function sendEmail(auth) {
  const gmail = google.gmail('v1');

  const base64EncodedEmail = Base64.encodeURI(email);
  base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_')
  console.log(base64EncodedEmail);

  gmail.users.messages.send({
    auth: auth,
    userId: "me",
    resource: {
      raw: base64EncodedEmail
    }
  }, (err, response) => {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    console.log(response);
  });
}

You can picture auth as an object:

{
  transporter: ...,
  _certificateCache: ...,
  _certificateExpiry: ...,
  _clientId: ...,
  _clientSecret: ...,
  _redirectUri: ...,
  _opts: {},
  credentials: { 
    access_token: ...,
    refresh_token: ...,
    token_type: 'Bearer',
    expiry_date: 1517563087857 
  } 
}

What matters is the access_token.


I've already tried the solutions proposed which are listed here:

  • StackOverflow: Failed sending mail through google api with javascript
  • ExceptionsHub: Failed sending mail through google api in nodejs
  • StackOverflow: Gmail API for sending mails in Node.js

But none of them worked. However, when I copied and pasted the encoded string onto the Playground of Google's own documentation, and it works (documentation):

Therefore, I changed to using a fetch request instead, and it also worked.

fetch(`https://www.googleapis.com/gmail/v1/users/me/messages/send`, {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer ' + `the_access_token_in_auth_obj`,
    'HTTP-Version': 'HTTP/1.1',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    raw: base64EncodedEmail
  })
})
.then((res) => res.json())
.then((res) => console.info(res));

Can anyone explain why it happened? Is it a bug from googleapi or am I missing something?


回答1:


I ran into the same "RFC822 payload message string or uploading message via /upload/* URL required". The quickstart/nodejs sample specifies a version of google-auth-library that caused this error. The quickstart specifies:

npm install google-auth-library@0.* --save

When I changed this to

npm install google-auth-library -- save

it pulled in version 1.3.1 vs 0.12.0. Everything started working once I changed the code to account for the breaking changes. The latest version of googleapis also has breaking changes. Here is my tweaks to the quickstart:

package.json

 ....
  "dependencies": {
    "google-auth-library": "^1.3.1",
    "googleapis": "^26.0.1"
  }

quickstart.js

var fs = require('fs');
var readline = require('readline');
var {google} = require('googleapis');
const {GoogleAuth, JWT, OAuth2Client} = require('google-auth-library');


var SCOPES = [
    'https://mail.google.com/',
    'https://www.googleapis.com/auth/gmail.modify',
    'https://www.googleapis.com/auth/gmail.compose',
    'https://www.googleapis.com/auth/gmail.send'
];

var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
    process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'gmail-nodejs-quickstart.json';

function authorize(credentials, callback) {
    var clientSecret = credentials.installed.client_secret;
    var clientId = credentials.installed.client_id;
    var redirectUrl = credentials.installed.redirect_uris[0];
    var auth = new GoogleAuth();
    var oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);

    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, function (err, token) {
        if (err) {
            getNewToken(oauth2Client, callback);
        } else {
            oauth2Client.credentials = JSON.parse(token);
            callback(oauth2Client);
        }
    });
}

function getNewToken(oauth2Client, callback) {
    var authUrl = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES
    });
    console.log('Authorize this app by visiting this url: ', authUrl);
    var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    rl.question('Enter the code from that page here: ', function (code) {
        rl.close();
        oauth2Client.getToken(code, function (err, token) {
            if (err) {
                console.log('Error while trying to retrieve access token', err);
                return;
            }
            oauth2Client.credentials = token;
            storeToken(token);
            callback(oauth2Client);
        });
    });
}


function makeBody(to, from, subject, message) {
    var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
        "MIME-Version: 1.0\n",
        "Content-Transfer-Encoding: 7bit\n",
        "to: ", to, "\n",
        "from: ", from, "\n",
        "subject: ", subject, "\n\n",
        message
    ].join('');

    var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
        return encodedMail;
}

function sendMessage(auth) {
    var gmail = google.gmail('v1');
    var raw = makeBody('xxxxxxxx@hotmail.com', 'xxxxxxx@gmail.com', 'test subject', 'test message');
    gmail.users.messages.send({
        auth: auth,
        userId: 'me',
        resource: {
            raw: raw
        }
    }, function(err, response) {
        console.log(err || response)
    });
}

const secretlocation = 'client_secret.json'

fs.readFile(secretlocation, function processClientSecrets(err, content) {
    if (err) {
        console.log('Error loading client secret file: ' + err);
        return;
    }
    // Authorize a client with the loaded credentials, then call the
    // Gmail API.
    authorize(JSON.parse(content), sendMessage);
});

Now when I run, I get the response

Object {status: 200, statusText: "OK", headers: Object, config: Object, request: ClientRequest, …}



回答2:


Adding to @grabbag's answer, which excluded the definition for store_token. As the Drive quickstart notes, that function can be defined as follows:

/**
 * Store token to disk be used in later program executions.
 *
 * @param {Object} token The token to store to disk.
 */
function storeToken(token) {
  try {
    fs.mkdirSync(TOKEN_DIR);
  } catch (err) {
    if (err.code != 'EEXIST') {
      throw err;
    }
  }
  fs.writeFile(TOKEN_PATH, JSON.stringify(token));
  console.log('Token stored to ' + TOKEN_PATH);
}


来源:https://stackoverflow.com/questions/48579566/failed-sending-mail-through-google-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!