DevError404
DevError404

Reputation: 147

Adding Users Email to read specific email from their inbox using Gmail API

I have a requirement in which I want to read specific emails from inbox of the users of my applications. I have created a project in google console and add Gmail API. Now I am able to read my own emails using it. but I am not getting how can I authenticate my users and will be able to read their emails from my application. My Application is in Nodejs.

For every operation I want to perform for another user I am getting :

The API returned an error: Error: Delegation denied for

fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading client secret file:', err);
    // Authorize a client with credentials, then call the Gmail API.
    authorize(JSON.parse(content), getRecentEmail);
  });

function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getNewToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

function getRecentEmail(auth) {
   
  // Only get the recent email - 'maxResults' parameter
  gmail.users.messages.list({auth: auth, userId: 'me',  maxResults: 10,}, function(err, response) {
      if (err) {
          console.log('The API returned an error: ' + err);
          return;
      }
    
    // Get the message id which we will need to retreive tha actual message next.
    var message_id = response['data']['messages'][0]['id'];
    // Retreive the actual message using the message id
    //https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/delegates
    gmail.users.messages.get({auth: auth, userId: 'me', 'id': message_id}, function(err, response) {
        if (err) {
            console.log('The API returned an error: ' + err);
            return;
        }
       
       console.log(response['data']);
      let message_raw = response['data']['payload']['parts'][0].body.data;

     let data = message_raw;  
    let buff = new Buffer(data, 'base64');  
    let text = buff.toString();

    });
  });
}

If I am adding any other mail id in UserId i am getting the error."The API returned an error: Error: Delegation denied for "

Please help me out or suggest if I can use something else to achieve this.

Upvotes: 1

Views: 410

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116908

When you authorize your application it is authorized for the user who consented to your access. If you want to access data of a different user then you will need to authorize your application as that user.

You cant be authorized to access more then one users data at a time unless you create multiple authorization connections.

If you are getting The API returned an error: Error: Delegation denied for it makes me think you are trying to do this with a service account. If you are then you need to set up the service account in the Google workspace account and grant it access to access the data on behalf of each user. Then you can delegate your service account to access that users data.

Upvotes: 1

Related Questions