title: Java Refreshing Auth token after 30 min
body: <p>I want to know how to keep refreshing token every 30 min. Currently i dont have it. I need to cache that token for 30 min and then replace current token with the new refresh token.</p>
  
  <p>Right now when i pass 1000 records all records uses same Auth token but the program runs more than 1 hour. I get Auth token expired error for some of the records.</p>
  
  <p>Can anyone help me with how to handle that scenario ?</p>
  
  <p>Thanks in advance</p>
  
  <p>This is call class and will be calling token class.</p>
  
  <pre><code>    package main.java.com.test;
      import java.io.IOException;
      import java.io.PrintStream;
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.time.LocalDateTime;
      import java.util.concurrent.TimeUnit;
  
      import org.apache.pig.EvalFunc;
      import org.apache.pig.data.Tuple;
      import com.google.common.base.Stopwatch;
  
      public class Call {
      private final String USER_AGENT = "Mozilla/5.0";
  
      public static void main(String[] args) throws IOException
      {
          new Call().execute();
      }
  
      public String execute() throws IOException {
          String number = "01";
          String id = "0123456789";
          String cd = "107BC0000X";
  
          Token getToken = new Token();
  
          String token = null;
  
  
          try {
              token = getToken.Token();
          } catch (Exception e1) {
              e1.printStackTrace();
          }
          System.out.println("access token" + token);
  
          return token;
      }
      }
  </code></pre>
  
  <p>This is another class called Token</p>
  
  <pre><code>package main.java.com.test;
  
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.List;
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.TrustManager;
  import javax.net.ssl.X509TrustManager;
  
  import org.apache.http.Header;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  
  import org.apache.http.conn.ClientConnectionManager;
  import org.apache.http.conn.scheme.Scheme;
  import org.apache.http.conn.scheme.SchemeRegistry;
  import org.apache.http.conn.ssl.SSLSocketFactory;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.codehaus.jettison.json.JSONObject;
  
  
  
     public class Token {
  
      private final String USER_AGENT = "Mozilla/5.0";
  
      public String Token() throws Exception
      {
          Token http = new Token();
  
          http.sendGet();
  
          String token = http.sendPost();
  
          return token;
      }
  
      private String sendPost() throws  Exception
      {
  
          String url = "https://YOUR_AUTH0_DOMAIN/oauth/token";
  
          HttpClient client = new DefaultHttpClient();
          HttpClient httpClient1 = wrapClient(client);
          HttpPost post = new HttpPost(url);
  
          post.setHeader("User-Agent" , "Mozilla/5.0");
  
          List urlParam =  (List) new ArrayList();
          urlParam.add(new BasicNameValuePair("client_id", ""));
          urlParam.add(new BasicNameValuePair("grant_type", ""));
          urlParam.add(new BasicNameValuePair("client_secret", ""));
  
         post.setEntity(new UrlEncodedFormEntity(urlParam));
  
          HttpResponse response = httpClient1.execute(post);
  
          BufferedReader rd = new BufferedReader(new 
          InputStreamReader(response.getEntity().getContent()));
  
          StringBuffer result = new StringBuffer();
          String line = "";
  
          while ((line = rd.readLine()) != null){
              result.append(line);
          }
  
          String[] JsonTags0 = result.toString().split(",");
          String[] JsonTags1 = result.toString().split(":");
          String token1 = JsonTags1[1].trim();
  
          return token1.substring(1,37);
  
      }
  
      private void sendGet()
      {
  
      }
  
      public static HttpClient wrapClient(HttpClient base)
      {
          try {
              SSLContext ctx = SSLContext.getInstance("TLS");
              X509TrustManager tm = new X509TrustManager1();
  
  
              ctx.init(null, new TrustManager[] {tm},null);
              SSLSocketFactory ssf = new SSLSocketFactory(ctx);
              ClientConnectionManager ccm = base.getConnectionManager(ctx , SSLSocketFactory , ALLOW_ALL_HOSTNAME_VERIFIER);
              SchemeRegistry sr = ccm.getSchemeRegistry();
              sr.register(new Scheme("https", ssf , 443));
              return new DefaultHttpClient(ccm, base.getParams());
  
          }
          catch (Exception ex){
              ex.printStackTrace();
              return null;
          }
      }
  }
  </code></pre>