Advertisement
nicb

Untitled

Jul 1st, 2020
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package com.alten.altenTrip.service;
  2.  
  3.  
  4. import lombok.Data;
  5. import org.springframework.core.env.Environment;
  6. import org.json.JSONObject;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.scheduling.annotation.Scheduled;
  9. import org.springframework.stereotype.Service;
  10.  
  11. import javax.net.ssl.HttpsURLConnection;
  12. import java.io.BufferedReader;
  13. import java.io.DataOutputStream;
  14. import java.io.InputStreamReader;
  15. import java.net.HttpURLConnection;
  16. import java.net.URL;
  17. import java.nio.charset.StandardCharsets;
  18. @Data
  19. @Service
  20. public class TokenScheduled {
  21.     @Autowired
  22.     private Environment env;
  23.     String token;
  24.  
  25.     @Scheduled( initialDelay = 1000L, fixedDelay = 180000L)
  26.     void genereteToken() throws Exception{
  27.  
  28.         HttpURLConnection con;
  29.         String clientID = env.getProperty("id.path");
  30.         String secretID = env.getProperty("secret.path");
  31.         var url = "https://test.api.amadeus.com/v1/security/oauth2/token";
  32.         var urlParameters = "grant_type=client_credentials&"+clientID+"&"+secretID;
  33.         System.out.println(urlParameters);
  34.         byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
  35.         var myurl = new URL(url);
  36.         con = (HttpsURLConnection) myurl.openConnection();
  37.         try {
  38.             con.setDoOutput(true);
  39.             con.setRequestMethod("POST");
  40.             con.setRequestProperty("User-Agent", "Java client");
  41.             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  42.  
  43.             try (var wr = new DataOutputStream(con.getOutputStream())) {
  44.  
  45.                 wr.write(postData);
  46.             }
  47.  
  48.             StringBuilder content;
  49.  
  50.             try (var br = new BufferedReader(
  51.                     new InputStreamReader(con.getInputStream()))) {
  52.  
  53.                 String line;
  54.                 content = new StringBuilder();
  55.  
  56.                 while ((line = br.readLine()) != null) {
  57.                     content.append(line);
  58.                     content.append(System.lineSeparator());
  59.                 }
  60.             }
  61.  
  62.             System.out.println(content.toString());
  63.             JSONObject jsonObj = new JSONObject(content.toString());
  64.             System.out.println(jsonObj.getString("access_token"));
  65.             token = jsonObj.getString("access_token");
  66.  
  67.         } finally {
  68.  
  69.             con.disconnect();
  70.         }
  71.  
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement