【发布时间】: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