【问题标题】:Gmail API: Connection RefusedGmail API:连接被拒绝
【发布时间】:2021-12-22 20:48:28
【问题描述】:

当从下面的类连续调用getCode()、getToken(code)和getEmailData(token)时,我收到错误POST http://localhost:9000/api/email net::ERR_CONNECTION_REFUSED on

const {
      data: { messagesTotal },
      errors,
    } = await this.gmailClient.users.getProfile({
      userId: "me",
    });

我无法弄清楚为什么会这样,而且在用户之间都是这种情况,有节流阀,还有列表标签功能。有谁知道可能是什么问题?

export default class GmailClient {
  authClient;
  gmailClient;
  SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"];
  constructor() {
    //grab the credentials from the json file
    const credentials = JSON.parse(
      fs.readFileSync("controllers/credentials.json").toString()
    );
    const { client_secret, client_id, redirect_uris } = credentials.web;

    this.authClient = new google.auth.OAuth2(
      client_id,
      client_secret,
      "http://localhost:3000"
    );
    this.gmailClient = google.gmail({ version: "v1", auth: this.authClient });
  }

  async getEmailData(token) {
    // no creds saved
    if (!this.authClient.credentials?.refresh_token) {
      this.authClient.setCredentials(token);
      this.gmailClient = google.gmail({ version: "v1", auth: this.authClient });
    }

    const {
      data: { messagesTotal },
      errors,
    } = await this.gmailClient.users.getProfile({
      userId: "me",
    });

    console.log("Error", errors);

    return { labels: [`Messages total ${messagesTotal}`] };
  }

  getCode() {
    const authUrl = this.authClient.generateAuthUrl({
      access_type: "offline",
      scope: this.SCOPES,
    });
    return { authUrl };
  }

  async getToken(code) {
    try {
      const { tokens } = await this.authClient.getToken(code);
      this.authClient.setCredentials(tokens);
      return { token: tokens };
    } catch (err) {
      console.log("Error: Bad User Code: ", err);
      return res.json({ token: null });
    }
  }
}

【问题讨论】:

    标签: javascript google-api gmail google-oauth connection-refused


    【解决方案1】:

    我的补丁正在使用

    axiosRetry(axios, {
      retries: 3,
      retryDelay: (retryCount) => {
        console.log(`Request retry attempt: ${retryCount}`);
        return retryCount * 2000;
      },
      retryCondition: (error) => {
        console.log("Retrying on error: ", error);
        return error || error.response.status === 503;
      },
    });
    

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多