<commit_before>// Adapted from https://github.com/parro-it/electron-google-oauth

import google from 'googleapis';
import { stringify } from 'querystring';
import fetch from 'node-fetch';
const GoogleOAuth2 = google.auth.OAuth2;

export default class OAuth2 {


  /*
   * Construct a new Google OAuth2 instance.
   *
   * @param {object} BrowserWindow - a require('browser-window') instance
   * @param {object} opts - oAuth options
   * @param {string} opts.clientId - oAuth client_id
   * @param {string} opts.clientSecret - oAuth client_secret
   * @param {array}  opts.scopes - Array of oAuth scope strings
   */

  constructor (opts) {
    this.opts = opts;
    this.client = new GoogleOAuth2(opts.clientId, opts.clientSecret, 'urn:ietf:wg:oauth:2.0:oob');
  }


  /*
   * Open a window and get the oAuth tokens.
   */

  async auth (BrowserWindow) {
    const authorizationCode = await this._getAuthorizationCode(BrowserWindow);

    const data = stringify({
      code: authorizationCode,
      client_id: this.opts.clientId,
      client_secret: this.opts.clientSecret,
      grant_type: 'authorization_code',
      redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
    });

    const res = await fetch('https://accounts.google.com/o/oauth2/token', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: data
    });

    const resp = await res.json();
    this.token = resp;
    this.token.created = new Date().getTime();
    this.token.expires_at = this.token.created + resp.expires_in * 1000;
    this.client.setCredentials(this.token);
    return resp;
  }


  /*
   * Refresh the token
   */

  async refresh (token) {
    const data = stringify({
      refresh_token: token ? token.refresh_token : this.token.refresh_token,
      client_id: this.opts.clientId,
      client_secret: this.opts.clientSecret,
      grant_type: 'refresh_token'
    });

    const res = await fetch('https://www.googleapis.com/o/oauth2/token', {
      method: 'post',
      headers: {
        'Accept': 'application/json',<commit_msg>Fix oauth 2 refresh<commit_after>// Adapted from https://github.com/parro-it/electron-google-oauth

import google from 'googleapis';
import { stringify } from 'querystring';
import fetch from 'node-fetch';
const GoogleOAuth2 = google.auth.OAuth2;

export default class OAuth2 {


  /*
   * Construct a new Google OAuth2 instance.
   *
   * @param {object} BrowserWindow - a require('browser-window') instance
   * @param {object} opts - oAuth options
   * @param {string} opts.clientId - oAuth client_id
   * @param {string} opts.clientSecret - oAuth client_secret
   * @param {array}  opts.scopes - Array of oAuth scope strings
   */

  constructor (opts) {
    this.opts = opts;
    this.client = new GoogleOAuth2(opts.clientId, opts.clientSecret, 'urn:ietf:wg:oauth:2.0:oob');
  }


  /*
   * Open a window and get the oAuth tokens.
   */

  async auth (BrowserWindow) {
    const authorizationCode = await this._getAuthorizationCode(BrowserWindow);

    const data = stringify({
      code: authorizationCode,
      client_id: this.opts.clientId,
      client_secret: this.opts.clientSecret,
      grant_type: 'authorization_code',
      redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
    });

    const res = await fetch('https://accounts.google.com/o/oauth2/token', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: data
    });

    const resp = await res.json();
    this.token = resp;
    this.token.created = new Date().getTime();
    this.token.expires_at = this.token.created + resp.expires_in * 1000;
    this.client.setCredentials(this.token);
    return resp;
  }


  /*
   * Refresh the token
   */

  async refresh (token) {
    const data = stringify({
      refresh_token: token ? token.refresh_token : this.token.refresh_token,
      client_id: this.opts.clientId,
      client_secret: this.opts.clientSecret,
      grant_type: 'refresh_token'
    });

    const res = await fetch('https://www.googleapis.com/oauth2/v3/token', {
      method: 'post',
      headers: {
        'Accept': 'application/json',
{'fraction_non_alphanumeric': 0.1288502233717376, 'fraction_numerical': 0.0075241006348459915, 'mean_word_length': 2.9906191369606003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, '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': '4354843', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1343, 'n_words': 306}