-
Notifications
You must be signed in to change notification settings - Fork 2k
Open
Labels
api: gmailIssues related to the Gmail API API.Issues related to the Gmail API API.type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.
Description
I want to implement watch for new mails in INBOX for a Google Workspace email ID.
Already Verified Pointers:
- I've made sure that
client_idfromservice.jsonfile has domain wide delegated authorisation for the scope being used. - I've also made sure that service account has
Publisheraccess. - I've also made sure that
client_emailhasPublisheraccess from thePub/SubFollowed this solution
Still getting
{
message: 'Error sending test message to Cloud PubSub projects/<PROJECT_ID>/topics/gmail-watcher : User not authorized to perform this action.',
domain: 'global',
reason: 'forbidden'
}Code:
import { google, Auth } from 'googleapis';
import { resolve } from 'path';
const serviceAccountPath = resolve('./service.json')
const scopes = [
'https://www.googleapis.com/auth/gmail.metadata'
]
const emailToBeDelegated = '[email protected]'
class GoogleAuth {
public auth;
constructor(serviceAccountPath: string, scopes: string[], emailToBeDelegated: string){
this.auth = this.getAuth(serviceAccountPath, scopes, emailToBeDelegated);
}
public async getAuthorizedJWT () {
await this.auth.authorize();
return this.auth;
};
private getAuth = (serviceAccountPath: string, scopes: string[], emailToBeDelegated: string): Auth.JWT => {
return new Auth.JWT({
keyFile: serviceAccountPath,
scopes,
subject: emailToBeDelegated
});
};
}
class GMailService extends GoogleAuth {
constructor(serviceAccountPath: string, scopes: string[], emailToBeDelegated: string){
super(serviceAccountPath, scopes, emailToBeDelegated);
}
watch = async () => {
const auth = await this.getAuthorizedJWT();
return google.gmail({ version: 'v1' }).users.watch({auth,
userId: 'me',
requestBody: {
topicName: 'projects/<PROJECT_ID>/topics/gmail-watcher',
labelIds: ['INBOX']
}
})
}
}
(async () => {
const gMailService = new GMailService(serviceAccountPath, scopes, emailToBeDelegated);
console.log(await gMailService.watch());
})();Metadata
Metadata
Assignees
Labels
api: gmailIssues related to the Gmail API API.Issues related to the Gmail API API.type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.