I want to write a bot replying to some income messages of my gmail inbox. It would be nice if I can use it with an address with a plus sign: "myname+foo@gmail.com"
- What type of credentials do I need: OAuth with client secret or service account? Currently I try my way with a service account.
- I try to manage my email from a service account this way:
'use strict';
const Google = require('googleapis');
const Gmail = Google.gmail('v1');
const key = require('./secrets/file-from-service-account.json');
const jwtClient = new Google.auth.JWT(
key.client_email,
null,
key.private_key,
[
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.readonly',
],
'myemail@gmail.com' // Not a service account email, but my gmail account.
);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
Gmail.users.messages.list({
userId: 'myemail@gmail.com',
auth: jwtClient,
}, (err, response) => {
if (err) {
console.log('The API returned an error:', err)
return;
}
console.log('RESPONSE', response);
});
});
Here is the output:
$ node index.js
Error: unauthorized_client
at Request._callback (/home/ilyaigpetrov/Repos/ac-gmail-bot/node_modules/gtoken/lib/index.js:215:34)
at Request.self.callback (/home/ilyaigpetrov/Repos/ac-gmail-bot/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/home/ilyaigpetrov/Repos/ac-gmail-bot/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/home/ilyaigpetrov/Repos/ac-gmail-bot/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:292:16)
at emitNone (events.js:91:20)
Why does this error happen? Do I need to give Service Account a permission to use myemail@gmail.com somehow?
via ilyaigpetrov
No comments:
Post a Comment