<commit_before>var path = require('path');
var fs = require('fs');

var closureBasePath = path.join(__dirname, '/ext/closure-library/closure/goog' + path.sep);
var goog = require('closure').Closure({CLOSURE_BASE_PATH: closureBasePath});

var google = require('googleapis');
goog.require('goog.Uri');

goog.provide('gee');
goog.provide('gee.initialize');

// constants
var HOME = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var AUTH_FILE = path.join(__dirname, 'config/authinfo.json');
var REFRESH_TOKEN_FILE = HOME + '/.config/earthengine/credentials';

initialize = function (onsuccess) {
    var o = JSON.parse(fs.readFileSync(AUTH_FILE, 'utf8'));
    var client = new google.auth.OAuth2(o.client_id, o.client_secret, o.redirect_uri);

    function init() {
        var o2 = JSON.parse(fs.readFileSync(REFRESH_TOKEN_FILE, 'utf8'));
        client.setCredentials({refresh_token: o2.refresh_token});

        client.refreshAccessToken(function (err, tokens) {
            ee.data.authToken_ = 'Bearer ' + tokens['access_token'];
            ee.data.authClientId_ = o.cliet_id;
            ee.data.authScopes_ = [ee.data.AUTH_SCOPE_];
            ee.data.DEFAULT_API_BASE_URL_ = "https://earthengine.googleapis.com/api";
            ee.initialize(ee.data.DEFAULT_API_BASE_URL_);

            onsuccess();
        });
    }

    // check if refresh token exists
    var isEmptyRefreshToken = true;
    if(fs.existsSync(REFRESH_TOKEN_FILE)) {
        console.log('file exists')
        var o2 = JSON.parse(fs.readFileSync(REFRESH_TOKEN_FILE, 'utf8'));
        if('refresh_token' in Object.keys(o2)) {
            console.log('key exists')
            isEmptyRefreshToken = false;
        }
    }

    // generate refresh token
    if (isEmptyRefreshToken) {
        var toQueryData = function (params) {
            var queryData = new goog.Uri.QueryData();
            for (var item in params) {
                queryData.set(item, params[item]);
            }
            return queryData;
        };

        var params = {
            'scope': 'https://www.googleapis.com/auth/earthengine.readonly',
            'redirect_uri': o.redirect_uri,
            'response_type': 'code',
            'client_id': o.client_id
        };

        var uri = 'https://accounts.google.com/o/oauth2/auth?' + toQueryData(params).toString();

        //goog.Uri.create('https', null, 'accounts.google.com', null, '/o/oauth2/auth', toQueryData(params));

        // open browser to get authorization code
        var opener = require('opener');
        opener(uri);

        console.log("If browser does not open, use the following URL to get access code:");
        console.log(uri);
        console.log();

        // ask user to enter authorization code
        console.log('Please enter authorization code: ');
        readKey(function (auth_code) {
            console.log('Entered code: ' + auth_code);

            // request refresh token
            params = {
                'code': auth_code,
                'client_id': o.client_id,
                'client_secret': o.client_secret,
                'redirect_uri': o.redirect_uri,
                'grant_type': 'authorization_code'
            };
            console.log('https://accounts.google.com/o/oauth2/token?' + toQueryData(params).toString());

            var refresh_token = null;
            var request = require('request');
            request.post('https://accounts.google.com/o/oauth2/token', {form: params}, function optionalCallback(err, httpResponse, body) {
                if (err) {
                    return console.error('upload failed:', err);
                }
                refresh_token = JSON.parse(body).refresh_token;

                require('mkdirp').sync(require('path').dirname(REFRESH_TOKEN_FILE));

                // write refresh_token to config file<commit_msg>fix auth bug<commit_after>var path = require('path');
var fs = require('fs');

var closureBasePath = path.join(__dirname, '/ext/closure-library/closure/goog' + path.sep);
var goog = require('closure').Closure({CLOSURE_BASE_PATH: closureBasePath});

var google = require('googleapis');
goog.require('goog.Uri');

goog.provide('gee');
goog.provide('gee.initialize');

// constants
var HOME = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var AUTH_FILE = path.join(__dirname, 'config/authinfo.json');
var REFRESH_TOKEN_FILE = HOME + '/.config/earthengine/credentials';

initialize = function (onsuccess) {
    var o = JSON.parse(fs.readFileSync(AUTH_FILE, 'utf8'));
    var client = new google.auth.OAuth2(o.client_id, o.client_secret, o.redirect_uri);

    function init() {
        var refresh_token = JSON.parse(fs.readFileSync(REFRESH_TOKEN_FILE, 'utf8')).refresh_token;

        client.setCredentials({refresh_token: refresh_token});

        client.refreshAccessToken(function (err, tokens) {
            ee.data.authToken_ = 'Bearer ' + tokens.access_token;
            ee.data.authClientId_ = o.cliet_id;
            ee.data.authScopes_ = [ee.data.AUTH_SCOPE_];
            ee.data.DEFAULT_API_BASE_URL_ = "https://earthengine.googleapis.com/api";
            ee.initialize(ee.data.DEFAULT_API_BASE_URL_, null, onsuccess);
        });
    }

    // check if refresh token exists
    var isEmptyRefreshToken = true;
    if(fs.existsSync(REFRESH_TOKEN_FILE)) {
        var o2 = JSON.parse(fs.readFileSync(REFRESH_TOKEN_FILE, 'utf8'));
        if(o2.hasOwnProperty('refresh_token')) {
            isEmptyRefreshToken = false;
        }
    }

    // generate refresh token
    if (isEmptyRefreshToken) {
        var toQueryData = function (params) {
            var queryData = new goog.Uri.QueryData();
            for (var item in params) {
                queryData.set(item, params[item]);
            }
            return queryData;
        };

        var params = {
            'scope': 'https://www.googleapis.com/auth/earthengine.readonly',
            'redirect_uri': o.redirect_uri,
            'response_type': 'code',
            'client_id': o.client_id
        };

        var uri = 'https://accounts.google.com/o/oauth2/auth?' + toQueryData(params).toString();

        //goog.Uri.create('https', null, 'accounts.google.com', null, '/o/oauth2/auth', toQueryData(params));

        // open browser to get authorization code
        var opener = require('opener');
        opener(uri);

        console.log("If browser does not open, use the following URL to get access code:");
        console.log(uri);
        console.log();

        // ask user to enter authorization code
        console.log('Please enter authorization code: ');

        var done = false
        readKey(function (auth_code) {
            if(done) {
              return
            }

            done = true

            console.log('Entered code: ' + auth_code);

            // request refresh token
            params = {
                'code': auth_code,
                'client_id': o.client_id,
                'client_secret': o.client_secret,
                'redirect_uri': o.redirect_uri,
                'grant_type': 'authorization_code'
            };
            console.log('https://accounts.google.com/o/oauth2/token?' + toQueryData(params).toString());

            var refresh_token = null;
            var request = require('request');
            request.post('https://accounts.google.com/o/oauth2/token', {form: params}, function optionalCallback(err, httpResponse, body) {
                if (err) {
                    return console.error('upload failed:', err);
                }
                refresh_token = JSON.parse(body).refresh_token;

                require('mkdirp').sync(require('path').dirname(REFRESH_TOKEN_FILE));

                // write refresh_token to config file
{'fraction_non_alphanumeric': 0.1330598641199846, 'fraction_numerical': 0.003332906037687476, 'mean_word_length': 2.3499355946758267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7610097', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2253, 'n_words': 439}