Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alten.altenTrip.service;
- import lombok.Data;
- import org.springframework.core.env.Environment;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import javax.net.ssl.HttpsURLConnection;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- @Data
- @Service
- public class TokenScheduled {
- @Autowired
- private Environment env;
- String token;
- @Scheduled( initialDelay = 1000L, fixedDelay = 180000L)
- void genereteToken() throws Exception{
- HttpURLConnection con;
- String clientID = env.getProperty("id.path");
- String secretID = env.getProperty("secret.path");
- var url = "https://test.api.amadeus.com/v1/security/oauth2/token";
- var urlParameters = "grant_type=client_credentials&"+clientID+"&"+secretID;
- System.out.println(urlParameters);
- byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
- var myurl = new URL(url);
- con = (HttpsURLConnection) myurl.openConnection();
- try {
- con.setDoOutput(true);
- con.setRequestMethod("POST");
- con.setRequestProperty("User-Agent", "Java client");
- con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- try (var wr = new DataOutputStream(con.getOutputStream())) {
- wr.write(postData);
- }
- StringBuilder content;
- try (var br = new BufferedReader(
- new InputStreamReader(con.getInputStream()))) {
- String line;
- content = new StringBuilder();
- while ((line = br.readLine()) != null) {
- content.append(line);
- content.append(System.lineSeparator());
- }
- }
- System.out.println(content.toString());
- JSONObject jsonObj = new JSONObject(content.toString());
- System.out.println(jsonObj.getString("access_token"));
- token = jsonObj.getString("access_token");
- } finally {
- con.disconnect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement