private Map<String, String> loadAllToks(final String code, int port, final HttpClient httpClient)
      throws IOException {
    final HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
    try {
      final List<? extends NameValuePair> nvps =
          Arrays.asList(
              new BasicNameValuePair("code", code),
              new BasicNameValuePair("client_id", model.getSettings().getClientID()),
              new BasicNameValuePair("client_secret", model.getSettings().getClientSecret()),
              new BasicNameValuePair("redirect_uri", OauthUtils.getRedirectUrl(port)),
              new BasicNameValuePair("grant_type", "authorization_code"));
      final HttpEntity entity = new UrlEncodedFormEntity(nvps, LanternConstants.UTF8);
      post.setEntity(entity);

      log.debug("About to execute post!");
      final HttpResponse response = httpClient.execute(post);

      log.debug("Got response status: {}", response.getStatusLine());
      final HttpEntity responseEntity = response.getEntity();
      final String body = IOUtils.toString(responseEntity.getContent());
      EntityUtils.consume(responseEntity);

      final Map<String, String> oauthToks = JsonUtils.OBJECT_MAPPER.readValue(body, Map.class);
      log.debug("Got oath data: {}", oauthToks);
      return oauthToks;
    } finally {
      post.reset();
    }
  }
Exemple #2
0
  public static GoogleOAuth2Credentials getGoogleOauthCreds() {
    final Details secrets;
    try {
      secrets = OauthUtils.loadClientSecrets().getInstalled();
    } catch (final IOException e) {
      throw new Error("Could not load client secrets?", e);
    }
    final String clientId = secrets.getClientId();
    final String clientSecret = secrets.getClientSecret();

    return new GoogleOAuth2Credentials(
        "*****@*****.**",
        clientId,
        clientSecret,
        getAccessToken(),
        getRefreshToken(),
        "gmail.");
  }