Advertisement
Suzana_Marek

endpoint_enterPassword

Dec 2nd, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. import com.google.json.JsonObject;
  2. import com.google.json.JsonParser;
  3.  
  4. import java.io.OutputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.Scanner;
  8.  
  9. public class enterPassword {
  10.  
  11.     public static void main(String[] args) {
  12.         // Step 1: Ask the user to input their password
  13.         Scanner inputScanner = new Scanner(System.in);
  14.         System.out.print("Enter your password: ");
  15.         String password = inputScanner.nextLine();
  16.  
  17.         String username = "[email protected]"; // Hardcoded username for this example
  18.         String loginUrl = "https://dev.dripit.io/api/auth/login"; // Login API URL
  19.         String profileUrl = "https://dev.dripit.io/api/user/profile"; // User profile API URL
  20.  
  21.         // Step 2: Log in and get the token
  22.         String token = loginAndGetToken(loginUrl, username, password);
  23.         if (token != null) {
  24.             System.out.println("Login successful! Token received.");
  25.  
  26.             // Step 3: Use the token to fetch user profile
  27.             fetchUserProfile(profileUrl, token);
  28.         } else {
  29.             System.out.println("Login failed. Please check your username and password.");
  30.         }
  31.     }
  32.  
  33.     // Function to log in and get the token
  34.     public static String loginAndGetToken(String apiUrl, String username, String password) {
  35.         try {
  36.             // Open connection
  37.             URL url = new URL(apiUrl);
  38.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  39.             connection.setRequestMethod("POST");
  40.             connection.setRequestProperty("Content-Type", "application/json");
  41.             connection.setDoOutput(true);
  42.  
  43.             // Prepare JSON payload for login
  44.             String jsonInput = String.format("{\"username\": \"%s\", \"password\": \"%s\"}", username, password);
  45.  
  46.             // Send the JSON payload
  47.             try (OutputStream os = connection.getOutputStream()) {
  48.                 os.write(jsonInput.getBytes());
  49.                 os.flush();
  50.             }
  51.  
  52.             // Read the response
  53.             int responseCode = connection.getResponseCode();
  54.             if (responseCode == 200) { // Success
  55.                 try (Scanner scanner = new Scanner(connection.getInputStream())) {
  56.                     String response = scanner.useDelimiter("\\A").next();
  57.  
  58.                     // Parse JSON response to extract the token
  59.                     JsonObject jsonResponse = JsonParser.parseString(response).getAsJsonObject();
  60.                     return jsonResponse.get("token").getAsString(); // Assuming the token is in the "token" field
  61.                 }
  62.             } else {
  63.                 System.out.println("Error: Received response code " + responseCode);
  64.             }
  65.         } catch (Exception e) {
  66.             System.out.println("An error occurred during login: " + e.getMessage());
  67.         }
  68.  
  69.         return null; // Return null if login fails
  70.     }
  71.  
  72.     // Function to fetch user profile using the token
  73.     public static void fetchUserProfile(String apiUrl, String token) {
  74.         try {
  75.             // Open connection
  76.             URL url = new URL(apiUrl);
  77.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  78.             connection.setRequestMethod("GET");
  79.             connection.setRequestProperty("Authorization", "Bearer " + token); // Add token to the header
  80.  
  81.             // Read the response
  82.             int responseCode = connection.getResponseCode();
  83.             if (responseCode == 200) { // Success
  84.                 try (Scanner scanner = new Scanner(connection.getInputStream())) {
  85.                     String response = scanner.useDelimiter("\\A").next();
  86.  
  87.                     // Print the user profile data
  88.                     System.out.println("User Profile:");
  89.                     System.out.println(response);
  90.                 }
  91.             } else {
  92.                 System.out.println("Error: Unable to fetch profile. Response code " + responseCode);
  93.             }
  94.         } catch (Exception e) {
  95.             System.out.println("An error occurred while fetching the user profile: " + e.getMessage());
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement