Dont help him he is a SCAMMER >
2022-08-11_191448.png
@teamgame said in npm модуль для получения почты через Gmail api. Как настроить?:
Там надо закинуть client_secret.json в рабочую директорию
И где найти этот файл
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'gmail-nodejs-quickstart.json';
куда ставить все это понял
но после запуска node quickstart.js выдает ссылку копирую эту ссылку ставлю в браузер пишет ошибка
404. That’s an error.
не могу с этим разобраться
@teamgame said in npm модуль для получения почты через Gmail api. Как настроить?:
открываю все это командная строка мне выдает ссылку пишет открой эту ссылку и напиши в командную строку код который на странице а вот ссылка это не открывается google ошибку выдает
Нашел время для настройки работы с модулем gmail.
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://mail.google.com/'];
const TOKEN_PATH = 'token.json';
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), getRecentEmail);
});
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function getRecentEmail(auth) {
var gmail = google.gmail('v1');
gmail.users.messages.list({auth: auth, userId: 'me', maxResults: 3, q: 'is:unread'}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var message_id = response['data']['messages'][0]['id'];
gmail.users.messages.get({auth: auth, userId: 'me', 'id': message_id}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
console.log(response['data']);
});
gmail.users.messages.modify({
"auth": auth,
'userId':'me',
'id':message_id,
'resource': {
'addLabelIds':[],
'removeLabelIds': ['UNREAD']
}
}, function(err) {
if (err) {
console.log('Failed to mark email as read! Error: '+err);
return;
}
console.log('Successfully marked email as read', message_id);
});
});
}