我正在使用Nest.js進行編碼,現在我成功地獲取了電子郵件資料。現在,我想透過訊息ID逐一取得每封電子郵件,並檢查該電子郵件是否已讀或未讀。如果電子郵件未讀,我想將其標記為已讀並在資料庫中進行更新。 任何了解的人請為此重寫程式碼,
注意:我正在使用Prisma ORM進行資料庫操作
// google-sheet.service.ts import { Injectable } from '@nestjs/common'; import { google } from 'googleapis'; import { JWT } from 'google-auth-library'; import { EnvironmentService } from 'src/core/environments/environments.service'; import axios from 'axios'; @Injectable() export class GmailService { constructor(private environmentService: EnvironmentService) {} async getEMails() { try { const oAuth2Client = new google.auth.OAuth2( this.environmentService.clientId(), this.environmentService.clientSecret(), this.environmentService.googleUri(), ); await oAuth2Client.setCredentials({ refresh_token:this.environmentService.refresh_token() , }); // 获取所有id const fetchingIdUrl = `https://gmail.googleapis.com/gmail/v1/users/email@gmail.com/messages/`; const { token } = await oAuth2Client.getAccessToken(); const config: any = { headers: { Authorization: `Bearer ${token}`, }, }; const response = await axios.get(fetchingIdUrl, config); // 通过id获取邮件 const fetchingEmailById = `https://gmail.googleapis.com/gmail/v1/users/eamil@gmail.com/messages/${response.data.id}`; const emailResponse = await axios.get(fetchingEmailById, config); const emailData = response.data; console.log(emailData); // emailData.payload.parts.forEach((part, index) => { // if (part.body.size > 0) { // const bodyContent = Buffer.from(part.body.data, 'base64').toString(); // console.log('Body Content:'); // console.log(bodyContent); // } // }); } catch (err) { console.error('Error fetching emails:', err.message); } } }