What I want to do is to send user a mail, a user's password , from my node js rest api. I want to know how to send email using gmail api and api keys only.
Asked
Active
Viewed 2,916 times
0

Linda Lawton - DaImTo
- 106,405
- 32
- 180
- 449

Sushant Kumar
- 243
- 3
- 8
-
Google has an [API reference for gmail](https://developers.google.com/gmail/api/quickstart/nodejs) and even a section on how to use it from node.js. Have you read that? What code have you tried? Where did you get stuck. We don't just write code for people who don't show that they've even tried or describe anything about where they got stuck and what problem they ran into. – jfriend00 Mar 20 '17 at 07:10
-
I had tried that code but that code using 0auth id which force to authenticate by providing url from code what I want to do is to just send an email without authenticating the url but just using api keys which I have created in project google console. – Sushant Kumar Mar 20 '17 at 07:32
-
I think you have to authenticate. You can't use only API keys. An API key identifies your API usage, but doesn't authenticate you into the account. – jfriend00 Mar 20 '17 at 07:36
-
Google discontinued client login (login and password) on all of its APIs in 2015 you will have to go though the SMTP server. – Linda Lawton - DaImTo Mar 20 '17 at 08:02
-
@DaImTo what should all those extra **redundant** tags do something good for? `google-api` `gmail-api` `google-oauth2` `google-client-login` cannot be more overstuffed. It does not help anyone (but perhaps you, as a tag hunter) Please read **http://stackoverflow.com/help/tagging** – davidkonrad Mar 20 '17 at 08:36
-
@davidkonrad additional tags help others to find questions which may be related to others google-api covers all google apis which this question is valid for. This might help http://stackoverflow.com/help/tagging that being said removing google-oauth2 may not be a bad idea. – Linda Lawton - DaImTo Mar 20 '17 at 08:41
2 Answers
1
Google discontinued client login (login and password) on all of its APIs in 2015. You cant access a Google API with login and password. Your users will need to authenticate using Oauth2.
API key is used for accessing public data only. Gmail is private user data and requires you to have access in order to access the data.
If you must use the login and password then I suggest you try and go though the SMTP or IMAP servers. I am not a node dev sorry I can not help you with that.

Linda Lawton - DaImTo
- 106,405
- 32
- 180
- 449
0
If what you want is to send an email using Gmail API without the need for authentications you must use a service acount and use jwt to authenticate.
example code:
const google = require('googleapis');
const gmail = google.gmail('v1');
const key = require('./mailing-bd0ff2b11546.json')//service acount jwt auth
let jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://mail.google.com/'],
'<mail to suplant>'
);
function getMessagesList(data) {
return new Promise((resolve, reject) => {
jwtClient.authorize(function (err, tokens) {
if (err) {
console.error(err);
} else {
gmail.users.messages.list({
auth: jwtClient,
userId: 'me',
labelIds: 'INBOX'
}, (err, messageList) => {
...
...
...
Here are some links to documentation: Creating a service account