1

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.

2
  • pleases edit your question and include your code Commented Jul 15, 2021 at 11:12
  • Can you please review now Commented Jul 15, 2021 at 11:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can I someohow add authorization for new user registering my application to read their emails.
If your using the normal sample code your issue is with TOKEN_PATH its single user you need to fix it so that you create a new token file for each user that authorizes your application.
Yes thats what i am thinking so. I need to send URL to users and when they allow access we a token will generate but I am not getting any reference to achieve this. can you refer any link or article regarding this great help. Thankyou
Sorry doint have anything node.js is normally used for backend systems here in other words i would not use it to authorize users in this manner. Users would be authorized though a web application and then have the refresh token stored in a database along with their user id, then node.js would load from that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.