oauth2Client.getToken missing refresh_token
Asked Answered
B

2

11

I have a small express server that has two routes. Then it writes the json tokens to a file (I know very insecure). For some reason there's no refresh_token. In the docs theres a comment that offline for access_type gets refresh_token, which is set and it's still not working

access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)

Here's the express server, sorry if the promises throw anyone off.

var Promise = require("bluebird")
var express = require('express')
var app = express()

var google = require('googleapis')
var OAuth2 = google.auth.OAuth2
var clientSecrets = require("./client_secrets.json")
var oauth2Client = new OAuth2(clientSecrets.web.client_id, clientSecrets.web.client_secret, clientSecrets.web.redirect_uris[0]);
oauth2Client.getToken = Promise.promisify(oauth2Client.getToken)

var fs = Promise.promisifyAll(require("fs"))

app.get('/google', function (req, res) {
  var url = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: "https://www.googleapis.com/auth/drive"
  })
  return res.redirect(url)
})

app.get('/oauth2callback', function (req, res) {
  return oauth2Client.getToken(req.query.code).then(function(tokens){
    fs.writeFileAsync("./tokens.json", JSON.stringify(tokens), "utf8");
    return res.json(tokens)
  }).catch(function(err){
    return res.redirect("/google")
  })
})

var server = app.listen(3000, function () {})
Bottali answered 17/2, 2015 at 15:18 Comment(0)
B
21

The refresh_token is only sent when the user initially authorizes your app with their account. So it the getToken function only returns it the first time (because you should store it). Added approval_prompt: "force" to the generateAuthUrl options, and I can get it every time.

Bottali answered 17/2, 2015 at 15:36 Comment(4)
I really wish this was stated more clearly in the documentation. Been pulling my hair out for hours.Nunciature
@Nunciature when I finally found out how it worked I felt the need to post this and answer it and try to save others some time. Glad it helped.Bottali
I'm just leaving this here so that anyone else who's passing by with a bald head is looking for a more hands-on experience with the google api: OAuth Playground. I wish Google wouldd keep these links up front and include the required information in the reference section of the documentation . . .Taluk
approval_prompt: "force" no longer works. Instead, use prompt: "consent".Joniejonina
C
3

To be more precise, you only get a refresh_token back when the user saw the consent screen, AKA the list of scopes and the "Accept" button. Users will see this screen the first time they are asked to grant access, but upon subsequent trips through the OAuth flow that screen is skipped (assuming they haven't revoked access). However, you can force the user to see the consent screen again by passing &prompt=consent in the authorization URL:

var url = oauth2Client.generateAuthUrl({
  access_type: 'offline',
  scope: scopes,
  prompt: 'consent'
});
Cognomen answered 22/2, 2022 at 3:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.