Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.json.JsonObject;
- import com.google.json.JsonParser;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.Scanner;
- public class enterPassword {
- public static void main(String[] args) {
- // Step 1: Ask the user to input their password
- Scanner inputScanner = new Scanner(System.in);
- System.out.print("Enter your password: ");
- String password = inputScanner.nextLine();
- String loginUrl = "https://dev.dripit.io/api/auth/login"; // Login API URL
- String profileUrl = "https://dev.dripit.io/api/user/profile"; // User profile API URL
- // Step 2: Log in and get the token
- String token = loginAndGetToken(loginUrl, username, password);
- if (token != null) {
- System.out.println("Login successful! Token received.");
- // Step 3: Use the token to fetch user profile
- fetchUserProfile(profileUrl, token);
- } else {
- System.out.println("Login failed. Please check your username and password.");
- }
- }
- // Function to log in and get the token
- public static String loginAndGetToken(String apiUrl, String username, String password) {
- try {
- // Open connection
- URL url = new URL(apiUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setDoOutput(true);
- // Prepare JSON payload for login
- String jsonInput = String.format("{\"username\": \"%s\", \"password\": \"%s\"}", username, password);
- // Send the JSON payload
- try (OutputStream os = connection.getOutputStream()) {
- os.write(jsonInput.getBytes());
- os.flush();
- }
- // Read the response
- int responseCode = connection.getResponseCode();
- if (responseCode == 200) { // Success
- try (Scanner scanner = new Scanner(connection.getInputStream())) {
- String response = scanner.useDelimiter("\\A").next();
- // Parse JSON response to extract the token
- JsonObject jsonResponse = JsonParser.parseString(response).getAsJsonObject();
- return jsonResponse.get("token").getAsString(); // Assuming the token is in the "token" field
- }
- } else {
- System.out.println("Error: Received response code " + responseCode);
- }
- } catch (Exception e) {
- System.out.println("An error occurred during login: " + e.getMessage());
- }
- return null; // Return null if login fails
- }
- // Function to fetch user profile using the token
- public static void fetchUserProfile(String apiUrl, String token) {
- try {
- // Open connection
- URL url = new URL(apiUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- connection.setRequestProperty("Authorization", "Bearer " + token); // Add token to the header
- // Read the response
- int responseCode = connection.getResponseCode();
- if (responseCode == 200) { // Success
- try (Scanner scanner = new Scanner(connection.getInputStream())) {
- String response = scanner.useDelimiter("\\A").next();
- // Print the user profile data
- System.out.println("User Profile:");
- System.out.println(response);
- }
- } else {
- System.out.println("Error: Unable to fetch profile. Response code " + responseCode);
- }
- } catch (Exception e) {
- System.out.println("An error occurred while fetching the user profile: " + e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement