I've tried the soultion from this answer and it isn't working for me.
I've tried revoking access to my app and reauthorizing and it's not working either. Here is my auth code:
export function handleAuth() {
const oauth2Client = getOAuth2Client();
const url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: ['https://mail.google.com/'],
});
}
I take the URL returned from this and use it to auth my gmail account. Then I have the auth callback:
app.get('/oauth2callback', async (req, res) => {
const query = req.query;
const code = query.code as string;
const { tokens } = await oauth2Client.getToken(code);
oauth2Client.setCredentials({
refresh_token: tokens.refresh_token,
});
});
And I have a listener waiting for new tokens:
oauth2Client.on('tokens', async (tokens) => {
if (tokens.refresh_token) {
oauth2Client.setCredentials({
refresh_token: tokens.refresh_token,
});
if (tokens.access_token && tokens.refresh_token) {
const tokenRepo = getCustomRepository(GcpTokenRepository);
await tokenRepo.create({
log in my db...
});
}
}
});
Then when I try and run the watch
method so I can listen to emails:
async watch() {
const gmail = await this.authGmail(); // method that returns type Promise<gmail_v1.Gmail>
const res = await gmail.users.watch({
userId: 'me',
requestBody: {
labelIds: ['INBOX'],
topicName: `topic name`,
},
});
console.log(' Watch re-initialized!', res);
}
And this watch method throws the error: Error: No access, refresh token, API key or refresh handler callback is set
When I console log my auth
variable returned from google.auth.OAuth2()
I also notice the credentials
field is an empty object...
Am I missing anything here?