text: var fs = require('fs')
  var readline = require('readline')
  var { OAuth2Client } = require('google-auth-library')
  const debug = require('debug')('report.js')
  const { listGameWeek } = require('./report')
  
  // If modifying these scopes, delete your previously saved credentials
  // at ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json
  var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
  var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
      process.env.USERPROFILE) + '/.credentials/'
  var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json'
  
  // Load client secrets from a local file.
  fs.readFile('client_secret.json', function processClientSecrets (err, content) {
      if (err) {
          debug('Error loading client secret file: ' + err)
          return
      }
      // Authorize a client with the loaded credentials, then call the
      // Google Sheets API.
      authorize(JSON.parse(content), listGameWeek)
  })
  
  /**
   * Create an OAuth2 client with the given credentials, and then execute the
   * given callback function.
   *
   * @param {Object} credentials The authorization client credentials.
   * @param {function} callback The callback to call with the authorized client.
   */
  function authorize (credentials, callback) {
      const clientId = credentials.installed.client_id
      const clientSecret = credentials.installed.client_secret
      const redirectUrl = credentials.installed.redirect_uris[0]
      const oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl)
      // Check if we have previously stored a token.
      fs.readFile(TOKEN_PATH, function (err, token) {
          if (err) {
              console.error('token not found')
              getNewToken(oauth2Client, callback)
          } else {
              oauth2Client.credentials = JSON.parse(token)
              callback(oauth2Client)
          }
      })
  }
  
  /**
   * Get and store new token after prompting for user authorization, and then
   * execute the given callback with the authorized OAuth2 client.
   *
   * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
   * @param {getEventsCallback} callback The callback to call with the authorized
   *     client.
   */
  function getNewToken (oauth2Client, callback) {
      var authUrl = oauth2Client.generateAuthUrl({
          access_type: 'offline',
          scope: SCOPES
      })
      debug('Authorize this app by visiting this url: ', authUrl)
      var rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
      })
      rl.question('Enter the code from that page here: ', function (code) {
          rl.close()
          oauth2Client.getToken(code, function (err, token) {
              if (err) {
                  debug('Error while trying to retrieve access token', err)
                  return
              }
              oauth2Client.setCredentials(token)
              storeToken(token)
              // callback(oauth2Client);
              console.info('Tokens acquired.', token)
          })
      })
  }
  
  /**
   * Store token to disk be used in later program executions.
   *
   * @param {Object} token The token to store to disk.
   */
  function storeToken (token) {
      try {
          fs.mkdirSync(TOKEN_DIR)
      } catch (err) {
          if (err.code !== 'EEXIST') {
              throw err
          }
      }
  
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), () => {
          debug('Token stored to ' + TOKEN_PATH)
      })
  }
  