GoogleCloudPlatform / cloud-functions-gmail-nodejs

A demo app that processes Gmail messages with Cloud Functions
https://cloud.google.com
Apache License 2.0
53 stars 20 forks source link

Auth Issue #20

Open ibains007 opened 5 years ago

ibains007 commented 5 years ago

I am trying this example, however the code fails with

TypeError: Cannot read property 'code' of undefined at OAuth2Client.getTokenAsync (/srv/node_modules/googleapis-common/node_modules/google-auth-library/build/src/auth/oauth2client.js:112:27) at OAuth2Client.getToken (/srv/node_modules/googleapis-common/node_modules/google-auth-library/build/src/auth/oauth2client.js:103:18) at Promise (/srv/index.js:56:18) at new Promise () at exports.oauth2callback (/srv/index.js:55:10) at /worker/worker.js:783:7 at /worker/worker.js:766:11 at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickDomainCallback (internal/process/next_tick.js:219:9)

bcoe commented 5 years ago

@ibains007 could you share a snippet of code demonstrating how you're creating the client?

ibains007 commented 5 years ago

sure.. I am working through this example - https://cloud.google.com/blog/products/application-development/adding-custom-intelligence-to-gmail-with-serverless-on-gcp

exports.oauth2callback = (req, res) => { // Get authorization code from request const { code } = req.query.code;

// OAuth2: Exchange authorization code for access token return new Promise((resolve, reject) => { oauth.client.getToken(code, (err, token) => (err ? reject(err) : resolve(token)) ); }) .then((token) => { // Get user email (to use as a Datastore key) oauth.client.credentials = token; return Promise.all([token, oauth.getEmailAddress()]); }) .then(([token, emailAddress]) => { // Store token in Datastore return Promise.all([ emailAddress, oauth.saveToken(emailAddress) ]); }) .then(([emailAddress]) => { // Respond to request res.redirect(/initWatch?emailAddress=${querystring.escape(emailAddress)}); }) .catch((err) => { // Handle error console.error(err); res.status(500).send('Something went wrong; check the logs.'); }); };