<commit_before>package com.baulsupp.oksocial.facebook;

import com.baulsupp.oksocial.ConsoleHandler;
import com.baulsupp.oksocial.LocalServer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.repackaged.com.google.common.base.Throwables;
import com.google.common.base.Charsets;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class LoginAuthFlow {

  public static FacebookCredentials login(OkHttpClient client) {
    try {
      String clientId = System.console().readLine("Facebook Client Id: ");
      String clientSecret = new String(System.console().readPassword("Facebook Client Secret: "));
      String scopes = System.console().readLine("Scopes: ");

      LocalServer s = new LocalServer("localhost", 3000);

      try {
        String serverUri = s.getRedirectUri();

        String loginUrl = "https://www.facebook.com/dialog/oauth"
            + "?client_id=" + clientId
            + "&redirect_uri=" + serverUri
            + "&scope=" + scopes;

        ConsoleHandler.openLink(loginUrl);

        String code = s.waitForCode();

        String tokenUrl = "https://graph.facebook.com/v2.3/oauth/access_token"
            + "?client_id=" + clientId
            + "&redirect_uri=" + serverUri
            + "&client_secret=" + clientSecret
            + "&code=" + code;

        String shortTokenJson = makeRequest(client, tokenUrl);

        ObjectMapper mapper = new ObjectMapper();
        JsonNode tree = mapper.readTree(shortTokenJson);

        String shortToken = tree.get("access_token").asText();

        String exchangeUrl = "https://graph.facebook.com/oauth/access_token"
            + "?grant_type=fb_exchange_token"
            + "&client_id=" + clientId
            + "&client_secret=" + clientSecret
            + "&fb_exchange_token=" + shortToken;

        String longTokenBody = makeRequest(client, exchangeUrl);

        return new FacebookCredentials(parseExchangeRequest(longTokenBody));
      } finally {
        s.stop();
      }
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  private static String makeRequest(OkHttpClient client, String exchangeUri) throws IOException {
    Request request = new Request.Builder().url(exchangeUri).build();
    Response response = client.newCall(request).execute();

    try {
      if (!response.isSuccessful()) {
        throw new IllegalStateException("unable to request token");
      }

      return response.body().source().readString(Charsets.UTF_8);
    } finally {
      response.body().close();
    }
  }

  private static String parseExchangeRequest(String body) {
    String[] params = body.split("&");

    for (String p : params) {
      String[] parts = p.split("=");

      if (parts[0].equals("access_token")) {
        return parts[1];
      }
    }

    return null;
  }
}
<commit_msg>fix import<commit_after>package com.baulsupp.oksocial.facebook;

import com.baulsupp.oksocial.ConsoleHandler;
import com.baulsupp.oksocial.LocalServer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables;
import com.google.common.base.Charsets;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class LoginAuthFlow {

  public static FacebookCredentials login(OkHttpClient client) {
    try {
      String clientId = System.console().readLine("Facebook Client Id: ");
      String clientSecret = new String(System.console().readPassword("Facebook Client Secret: "));
      String scopes = System.console().readLine("Scopes: ");

      LocalServer s = new LocalServer("localhost", 3000);

      try {
        String serverUri = s.getRedirectUri();

        String loginUrl = "https://www.facebook.com/dialog/oauth"
            + "?client_id=" + clientId
            + "&redirect_uri=" + serverUri
            + "&scope=" + scopes;

        ConsoleHandler.openLink(loginUrl);

        String code = s.waitForCode();

        String tokenUrl = "https://graph.facebook.com/v2.3/oauth/access_token"
            + "?client_id=" + clientId
            + "&redirect_uri=" + serverUri
            + "&client_secret=" + clientSecret
            + "&code=" + code;

        String shortTokenJson = makeRequest(client, tokenUrl);

        ObjectMapper mapper = new ObjectMapper();
        JsonNode tree = mapper.readTree(shortTokenJson);

        String shortToken = tree.get("access_token").asText();

        String exchangeUrl = "https://graph.facebook.com/oauth/access_token"
            + "?grant_type=fb_exchange_token"
            + "&client_id=" + clientId
            + "&client_secret=" + clientSecret
            + "&fb_exchange_token=" + shortToken;

        String longTokenBody = makeRequest(client, exchangeUrl);

        return new FacebookCredentials(parseExchangeRequest(longTokenBody));
      } finally {
        s.stop();
      }
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  private static String makeRequest(OkHttpClient client, String exchangeUri) throws IOException {
    Request request = new Request.Builder().url(exchangeUri).build();
    Response response = client.newCall(request).execute();

    try {
      if (!response.isSuccessful()) {
        throw new IllegalStateException("unable to request token");
      }

      return response.body().source().readString(Charsets.UTF_8);
    } finally {
      response.body().close();
    }
  }

  private static String parseExchangeRequest(String body) {
    String[] params = body.split("&");

    for (String p : params) {
      String[] parts = p.split("=");

      if (parts[0].equals("access_token")) {
        return parts[1];
      }
    }

    return null;
  }
}

{'fraction_non_alphanumeric': 0.12368152432800272, 'fraction_numerical': 0.004083021435862538, 'mean_word_length': 2.9589225589225587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1217707', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1639, 'n_words': 333}