{"size": 1702, "ext": "js", "max_stars_repo_path": "src/sheetsApi.js", "max_stars_repo_name": "tizmagik/acorn-bot", "max_stars_repo_stars_event_min_datetime": "2021-01-30T13:00:18.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-30T13:00:18.000Z", "max_issues_repo_path": "src/sheetsApi.js", "max_issues_repo_name": "tizmagik/acorn-bot", "max_issues_repo_issues_event_min_datetime": "2016-07-14T21:55:27.000Z", "max_issues_repo_issues_event_max_datetime": "2016-07-15T15:58:01.000Z", "max_forks_repo_path": "src/sheetsApi.js", "max_forks_repo_name": "tizmagik/acorn-bot", "max_forks_repo_forks_event_min_datetime": "2016-07-15T14:42:31.000Z", "max_forks_repo_forks_event_max_datetime": "2016-07-15T14:42:31.000Z"}
{"max_stars_count": 1, "max_issues_count": 6, "max_forks_count": 1, "avg_line_length": 30.3928571429, "max_line_length": 79, "alphanum_fraction": 0.7109283196}
var fs = require('fs');
var google = require('googleapis');
var googleAuth = require('google-auth-library');

module.exports = function(callback) {

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];


// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
  if (err) {
    var str = 'Error loading client secret file: ' + err;
    return;
  }
  // Authorize a client with the loaded credentials, then call the
  // Google Sheets API.
  authorize(JSON.parse(content), callback);
});

/**
 * 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) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];
  var auth = new googleAuth();
  var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
  // Check if we have previously stored a token.
  fs.readFile('token.json', function(err, token) {
    if (err) {
      oauth2Client.credentials = JSON.parse(process.env.API_TOKEN);
      storeToken(process.env.API_TOKEN);
      callback(oauth2Client);
    } else {
      oauth2Client.credentials = JSON.parse(token);
      callback(oauth2Client);
    }
  });
}

/**
 * Store token to disk be used in later program executions.
 *
 * @param {Object} token The token to store to disk.
 */
function storeToken(token) {
  fs.writeFile('token.json', token);
}

}