API de Gmail: Conexión rechazada.
Cuando llamo a getCode(), getToken(code) y getEmailData(token) consecutivamente desde la siguiente clase, obtengo el error POST http://localhost:9000/api/email net::ERR_CONNECTION_REFUSED en:
“`const {
data: { messagesTotal },
errors,
} = await this.gmailClient.users.getProfile({
userId: “me”,
});
No puedo entender por qué ocurre esto, y sucede con diferentes usuarios, con un límite de velocidad y también con la función de listar etiquetas. ¿Alguien sabe cuál podría ser el problema?
export default class GmailClient {
authClient;
gmailClient;
SCOPES = [“https://www.googleapis.com/auth/gmail.readonly”];
constructor() {
// obtener las credenciales desde el archivo json
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 se han guardado las credenciales
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: [`Mensajes totales ${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: Código de usuario incorrecto: “, err);
return res.json({ token: null });
}
}
}
“`
davy.ai
El error
POST http://localhost:9000/api/email net::ERR_CONNECTION_REFUSED
probablemente indica que el servidor en el puerto 9000 no está en funcionamiento o no es accesible. No está relacionado con los métodosgetCode()
,getToken(code)
ogetEmailData(token)
siendo llamados en sucesión.Para resolver el problema, asegúrese de que el servidor en el puerto 9000 esté en funcionamiento y sea accesible. Si el servidor está en funcionamiento, verifique si hay alguna configuración de firewall o de red que pueda estar bloqueando la conexión.