Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting a refresh_token back from Google provider #408

Closed
tomsansome opened this issue Jul 8, 2020 · 1 comment
Closed

Not getting a refresh_token back from Google provider #408

tomsansome opened this issue Jul 8, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@tomsansome
Copy link

tomsansome commented Jul 8, 2020

Describe the bug
When using just Google as a provider, I am not getting a refresh_token back. This means when I come to use the Google Sheets API, I give it the access_token, but that then expires after 1 hour.

I have read around all the issues and noticed that for Google a refresh token is only available on first login (#269), but I have tried the following but it is still returning null:

Apologies if I have missed something, this was my last resort opening an issue, I promise.

Any help truly appreciated. Thank you.

Versions tried:

  • 2.2.0
  • 3.0.0-beta.15

To Reproduce

  • Here are my next-auth options:
const options = {
  site: process.env.URL,
  providers: [
    Providers.Google({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
      params: {
        prompt: 'consent',
        access_type: 'offline',
        grant_type: 'authorization_code'
      },
      scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/spreadsheets.readonly'
    })
  ],
  database: process.env.DATABASE_URL,
  secret: process.env.NEXT_AUTH_SECRET,
  session: { jwt: true },
  debug: false,
  callbacks: {
    signin: async (profile, account, metadata) => {
      console.log('Profile: ', profile)
      console.log('Account: ', account)
      console.log('Meta: ', metadata)
    },
    session: async (session, token) => {
      const newSession = session

      newSession.user = token.user
      newSession.accessToken = token.account.accessToken

      return Promise.resolve(newSession)
    }
  }
}

Here is my _app.tsx:

export default ({ Component, pageProps }) => {
  return (
    <AppLayout>
      <Provider
        options={{
          clientMaxAge: 0,
          refreshInterval: 0
        }}
        session={pageProps.session}>
        <Component {...pageProps} />
      </Provider>
    </AppLayout>
  )
}

Expected behavior
When authenticating with Google, I get a refresh_token value back.

Screenshots or error logs
Screenshot of Mongo showing no refreshToken value:
Screenshot 2020-07-08 at 15 35 53

Additional context
I want to be able to use the refresh_token to rotate my access_token so that I can continually access the Google Sheets API. Here's how I'm using that incase that's helpful:

import { google } from 'googleapis'

const sheets = google.sheets('v4')

const sheet = await sheets.spreadsheets.values.get({
  spreadsheetId,
  range: sheetId,
  access_token: accessToken
})
@tomsansome tomsansome added the bug Something isn't working label Jul 8, 2020
@tomsansome
Copy link
Author

tomsansome commented Jul 10, 2020

After quite a lot of messing around, I ended up using the authorizationUrl with the params directly appended to the URL (not using the params option), and also using a different accessTokenUrl from the Google docs.

My next-auth options ended up looking like this:

const options = {
  site: process.env.URL,
  providers: [
    Providers.Google({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
      scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/spreadsheets.readonly',
      authorizationUrl: 'https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline&response_type=code',
      accessTokenUrl: 'https://oauth2.googleapis.com/token'
    })
  ],
  database: process.env.DATABASE_URL,
  secret: process.env.NEXT_AUTH_SECRET,
  debug: false
  }
}

This has now given me a refresh_token from Google

Screenshot 2020-07-10 at 15 14 03

🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant