googleapis / googleapis/google-api-nodejs-client

gmail.users.watch | error sending test message to Cloud PubSub projects/tms-erp-afourtech-assets/topics/gmail-watcher : User not authorized to perform this action.

Aperta
#2,843 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub
api: gmail type: question
Lingua principale
TypeScript
Stelle
12.2k
Fork
2k
Merge medio
1g 9h
PR unite (30g)
24

Descrizione

I want to implement watch for new mails in INBOX for a Google Workspace email ID.

Already Verified Pointers:
1. I've made sure that `client_id` from `service.json` file has domain wide delegated authorisation for the scope being used.
2. I've also made sure that service account has `Publisher` access.
3. I've also made sure that `client_email` has `Publisher` access from the [`Pub/Sub`](https://console.cloud.google.com/cloudpubsub/topic/list) [Followed this solution](https://stackoverflow.com/questions/68091042/gmailapi-error-sending-test-message-to-cloud-pubsub-projects-project-id-topi)

Still getting
```typescript
{
message: 'Error sending test message to Cloud PubSub projects//topics/gmail-watcher : User not authorized to perform this action.',
domain: 'global',
reason: 'forbidden'
}
```

Code:
```typescript
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.id@domain.com'

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//topics/gmail-watcher',
labelIds: ['INBOX']
}
})
}
}

(async () => {
const gMailService = new GMailService(serviceAccountPath, scopes, emailToBeDelegated);
console.log(await gMailService.watch());
})();
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start at the GMailService.watch method and the GoogleAuth.getAuth/getAuthorizedJWT methods shown in the issue. Reproduce the users.watch request with the listed Gmail scope and Pub/Sub topic, then determine which authorization boundary rejects it; done means identifying a confirmed cause and a reproducible resolution, since no repository file or test is named.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
api, authentication, cloud
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.