hatchways / team-vampire

1 stars 0 forks source link

Google OAuth2 Refresh Access Token upon Expiry #69

Open singhmi4 opened 3 years ago

singhmi4 commented 3 years ago

https://www.npmjs.com/package/passport-oauth2-refresh

singhmi4 commented 3 years ago

https://github.com/hatchways/team-vampire/tree/feature-be-passport-refresh

attempt at getting passport refresh to work after following the following the code implemented in the issue here: https://github.com/fiznool/passport-oauth2-refresh/issues/1

leaving this error message here so I can debug later:

(node:6050) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:482:11)
    at ServerResponse.header (/home/michael/Documents/webdev/projects/team-vampire/server/node_modules/express/lib/response.js:771:10)
    at ServerResponse.json (/home/michael/Documents/webdev/projects/team-vampire/server/node_modules/express/lib/response.js:264:10)
    at calendar.freebusy.query (/home/michael/Documents/webdev/projects/team-vampire/server/controllers/availability.js:85:38)
    at createAPIRequestAsync.then.r (/home/michael/Documents/webdev/projects/team-vampire/server/node_modules/googleapis-common/build/src/apirequest.js:48:53)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:6050) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6050) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
singhmi4 commented 3 years ago

hi @bonnieli, I tested the freebusy route after the accessToken expired, and I believe the refresh.requestAccessToken( ... ) function works since it returned the JSON of the available timeslots after going through the if (err) branch which provided the new access token and executed the getAvailableTimeSlots( ... ) callback again recursively which bypassed the if (err) branch.

Sever Console Output Below:

Database connected successfully
Method: GET
Path:   /api/avail/freebusy
Body:   {}
---
retries left 2
Free Busy Query Error:  { GaxiosError: invalid_request
    at Gaxios.<anonymous> (/home/michael/Documents/webdev/projects/team-vampire/server/node_modules/gaxios/build/src/gaxios.js:73:27)
    at Generator.next (<anonymous>)
    at fulfilled (/home/michael/Documents/webdev/projects/team-vampire/server/node_modules/gaxios/build/src/gaxios.js:16:58)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
  response:
   { config:
      { method: 'POST',
        url: 'https://oauth2.googleapis.com/token',
        data:
         'refresh_token=1%2F%2F04kfnNu37OKqyCgYIARAAGAQSNwF-L9IrpHkiXWMPX3NeXij0r9dDxkhYDs5-b0yuVL_Etjy2fPx0UAay3KQSaziPm2D54hdYBVE&client_id=&client_secret=cRH2vVlg-jcdto68q-Wr8Y9s&grant_type=refresh_token',
        headers: [Object],
        params: [Object: null prototype] {},
        paramsSerializer: [Function: paramsSerializer],
        body:
         'refresh_token=1%2F%2F04kfnNu37OKqyCgYIARAAGAQSNwF-L9IrpHkiXWMPX3NeXij0r9dDxkhYDs5-b0yuVL_Etjy2fPx0UAay3KQSaziPm2D54hdYBVE&client_id=&client_secret=cRH2vVlg-jcdto68q-Wr8Y9s&grant_type=refresh_token',
        validateStatus: [Function: validateStatus],
        responseType: 'json' },
     data:
      { error: 'invalid_request',
        error_description: 'Could not determine client ID from request.' },
     headers:
      { 'alt-svc':
         'h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
        'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
        connection: 'close',
        'content-encoding': 'gzip',
        'content-type': 'application/json; charset=utf-8',
        date: 'Tue, 15 Dec 2020 01:16:02 GMT',
        expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
        pragma: 'no-cache',
        server: 'scaffolding on HTTPServer2',
        'transfer-encoding': 'chunked',
        vary: 'Origin, X-Origin, Referer',
        'x-content-type-options': 'nosniff',
        'x-frame-options': 'SAMEORIGIN',
        'x-xss-protection': '0' },
     status: 400,
     statusText: 'Bad Request' },
  config:
   { method: 'POST',
     url: 'https://oauth2.googleapis.com/token',
     data:
      'refresh_token=1%2F%2F04kfnNu37OKqyCgYIARAAGAQSNwF-L9IrpHkiXWMPX3NeXij0r9dDxkhYDs5-b0yuVL_Etjy2fPx0UAay3KQSaziPm2D54hdYBVE&client_id=&client_secret=cRH2vVlg-jcdto68q-Wr8Y9s&grant_type=refresh_token',
     headers:
      { 'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'google-api-nodejs-client/3.1.2',
        Accept: 'application/json' },
     params: [Object: null prototype] {},
     paramsSerializer: [Function: paramsSerializer],
     body:
      'refresh_token=1%2F%2F04kfnNu37OKqyCgYIARAAGAQSNwF-L9IrpHkiXWMPX3NeXij0r9dDxkhYDs5-b0yuVL_Etjy2fPx0UAay3KQSaziPm2D54hdYBVE&client_id=&client_secret=cRH2vVlg-jcdto68q-Wr8Y9s&grant_type=refresh_token',
     validateStatus: [Function: validateStatus],
     responseType: 'json' },
  code: '400' }

// Request Access token
refresh token 1//04kfnNu37OKqyCgYIARAAGAQSNwF-L9IrpHkiXWMPX3NeXij0r9dDxkhYDs5-b0yuVL_Etjy2fPx0UAay3KQSaziPm2D54hdYBVE
ya29.a0AfH6SMDqs0F7t8F0uTI70gyfYWLXg5jD1wjLZ3i7XuKWoggjhoBMHFhELRBAlBHi-Q-OTkXKkOhDL5qSWn7UNNYY8awMFm5DXnx430geYXUCfv1JNsTigtdWuyPuMhaFkTnv99rFZ7qc2b3YwjZwzvbKuITmuQCH8yTrICgZtrC2
{ availabilities: [],
  meetingTypes:
   [ 5fcfe483c0135217f0de85d8,
     5fd024366245dc104f8e146e,
     5fd2972bbfee120cff8608a2 ],
  _id: 5fcfe447c0135217f0de85d7,
  googleId: '104348754318193001966',
  accessToken:
   'ya29.a0AfH6SMDqs0F7t8F0uTI70gyfYWLXg5jD1wjLZ3i7XuKWoggjhoBMHFhELRBAlBHi-Q-OTkXKkOhDL5qSWn7UNNYY8awMFm5DXnx430geYXUCfv1JNsTigtdWuyPuMhaFkTnv99rFZ7qc2b3YwjZwzvbKuITmuQCH8yTrICgZtrC2',
  refreshToken:
   '1//04kfnNu37OKqyCgYIARAAGAQSNwF-L9IrpHkiXWMPX3NeXij0r9dDxkhYDs5-b0yuVL_Etjy2fPx0UAay3KQSaziPm2D54hdYBVE',
  firstName: 'Michael',
  lastName: 'S',
  email: 'singh.michaelanthony@gmail.com',
  profilePicture:
   'https://lh3.googleusercontent.com/a-/AOh14GjAmMF8c19hWYlzl5fdYAyn5VS0iKNv95ugXtl_Pw=s96-c',
  createdAt: 2020-12-08T20:38:31.287Z,
  updatedAt: 2020-12-08T20:38:31.287Z,
  __v: 3 }

// Second Attempt which was Successful
retries left 1
[]