Nest JS에서 Gmail API를 사용하여 이메일을 읽음으로 표시하는 방법은 무엇입니까?
P粉818306280
P粉818306280 2024-01-10 18:00:25
0
1
424

코딩에 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);
        }
      }
    }

P粉818306280
P粉818306280

모든 응답(1)
P粉127901279

으아악

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!