byeokim / gmailpush

Gmail API push notification handler for Node.js
MIT License
53 stars 7 forks source link

A video example #4

Closed IRediTOTO closed 4 years ago

IRediTOTO commented 4 years ago

Hi, if you have time, can you make a video for this :( This too hard to use for me, I think.

byeokim commented 4 years ago

Sorry I can't. By the way could you tell me what problem are you having?

IRediTOTO commented 4 years ago

I am not sure, so hard to say. Google ping to my api every second. Your code look work but look like problem from my setup google. So I quit and trying to check email by call api every minutes from my backend... :(

byeokim commented 4 years ago

Was the req.body something like this?

{
  message: {
    data: 'eyJlbWFpbEFkZHJlc3MiOiJ1c2VyMUBnbWFpbC5jb20iLCJoaXN0b3J5SWQiOiI5ODc2NTQzMjEwIn0=',
    messageId: '1234567890',
    message_id: '1234567890',
    publishTime: '2020-07-18T00:11:23.000Z',
    publish_time: '2020-07-18T00:11:23.000Z'
  },
  subscription: 'projects/PROJECT_NAME/subscriptions/SUBSCRIPTION_NAME'
}
IRediTOTO commented 4 years ago

yes, right. I want google ping my api when it have email. That will make my life more easier. Still fight with cronjob here 🗡️

byeokim commented 4 years ago

Then what did console.log(err); print when you put your console.log(err); like this:

app.post(
  '/pubsub-push-endpoint',
  express.json(),
  (req, res) => {
    res.sendStatus(200);
    const email = gmailpush.getEmailAddress(req.body);
    const token = users.find((user) => user.email === email).token;
    gmailpush
      .getMessages({
        notification: req.body,
        token
      })
      .then((messages) => {
        console.log(messages);
      })
      .catch((err) => {
        console.log(err);
      });
  }
);
IRediTOTO commented 4 years ago

I do same with you, but gmailpush didnt show anything, just console.log("req.body", req.body); show the message you said

handler.post((req, res) => {
   res.status(200).end()
   console.log("req.body", req.body);  // just this show 
   const email = gmailpush.getEmailAddress(req.body);
   console.log("email", email);
   const token = users.find((user) => user.email === email).token;

   gmailpush
      .getMessages({
         notification: req.body,
         token,
         withLabelIds: ["UNREAD"],
      })
      .then((messages) => {
         console.log("messages ", messages);
      })
      .catch((err) => {
         console.error("error ", err);
      });
});

Look like gmailpush not run, no error or something. Hmm. image

IRediTOTO commented 4 years ago

btw, do I need to run email watch for pubsub?

gmail.users.watch({
    userId:"me",
    labelIds:["UNREAD"],
    topicName:"projects/checkmail-283408/topics/MyTopic"
})

When I run I got this: User not authorized to perform this action. I use my email and my email own all project :\ Why gg don't let me watch ?

IRediTOTO commented 4 years ago

Look like it worked. Thank you for push me. 💯 image

can you tell me how can authorized for an email? I just use my email access_token to run watch() to get notification image

byeokim commented 4 years ago

can you tell me how can authorized for an email? I just use my email access_token to run watch() to get notification

I don't understand what you meant. The following code will print out url for getting authorization from Gmail user. You can visit the url by yourself for your Gmail account or give it to other Gmail user for the user to authorize your app to receive notifications from the user's Gmail inbox.

const readline = require('readline');
const {google} = require('googleapis');

function getToken() {
  const auth = new google.auth.OAuth2(
    CLIENT_ID,
    CLIENT_SECRET,
    'urn:ietf:wg:oauth:2.0:oob'
  );

  const authUrl = auth.generateAuthUrl({
    access_type: 'offline',
    scope: ['https://www.googleapis.com/auth/gmail.readonly'],
  });

  console.log('Authorize this app by visiting this url:');
  console.log(authUrl);

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  rl.question('Enter the code from that page here: ', (authCode) => {
    rl.close();
    auth.getToken(authCode, (err, token) => {
      if (err) {
        return console.log('Error retrieving access token', err);
      }

      console.log('Token:');
      console.log(token);
    });
  });
}

getToken();