0

I have to create a small program in NodeJS that will check the user's account for new messages and notify them via SMS (using Twilio) if they have new mail. Unfortunately, I'm having troubles getting the fetch functionality to work.

I have searched for a while and all snippets I could find have the same snippet as the one I'm posting below:

var gmail = google.gmail('v1');
//console.log("auth is " + util.inspect(auth, false, null));
// Line 77 under this comment
gmail.users.messages.list({userId: 'me', auth: auth}, function(err, response) {
        console.log("this is " + util.inspect(response, false, null) + "with error " + err);
    });

When this runs, it crashes with:

/Users/mirtle/Documents/Universishit/smsmirtle/node_modules/googleapis/lib/apirequest.js:180 req = authClient.request(options, callback); ^

TypeError: authClient.request is not a function at createAPIRequest (/Users/mirtle/Documents/Universishit/smsmirtle/node_modules/googleapis/lib/apirequest.js:180:22) at Object.Gmail.self.users.messages.list (/Users/mirtle/Documents/Universishit/smsmirtle/node_modules/googleapis/apis/gmail/v1.js:728:16) at CronJob. (/Users/mirtle/Documents/Universishit/smsmirtle/server.js:77:26) at CronJob.fireOnTick (/Users/mirtle/Documents/Universishit/smsmirtle/node_modules/cron/lib/cron.js:392:22) at callbackWrapper [as _onTimeout] (/Users/mirtle/Documents/Universishit/smsmirtle/node_modules/cron/lib/cron.js:435:9) at Timer.listOnTimeout (timers.js:92:15)

Like I said all snippets I could find, including from other StackOverflow questions, are like this so I don't know what I'm doing wrong here.

The auth token is valid. If you uncomment the comment above my Line 77 comment it prints it properly. It also runs the provided example by Google to print labels and they get printed, so the token is valid and authorised.

As a curiosity, if I call the function like this:

gmail.users.messages.list({}, function(err, response){...}

With no parameters, it doesn't crash, but it complaints there's no userId.

I can also call it without the first parameter, but it also complaints about not having an userId.

I will appreciate any help you can give.

Sargento
  • 1
  • 3

2 Answers2

1

Instead of using directly the variable auth which contains the access_token and other data, you should set it as oauth credential first. I encounter same problem with you. What I did is (see code below)

oauth2Client.setCredentials(auth);
gmail.users.messages.list({userId: 'me', auth: oauth2Client}, function(err, response){

}

Then I successfully get all the email and thread ids. Hope this will help you.

sse
  • 47
  • 1
  • 10
0

It sounds like you are passing in the auth token, but you need to pass the auth client.

I'm talking about the first parameter to the list function: '{userId: 'me', auth: auth}'

Try making that the auth client that you used to get the token (eg. a google.auth.JWT).

Tom
  • 17,103
  • 8
  • 67
  • 75