Search code examples
javaauthenticationsoftware-designebay-api

How to make an online login in JavaFX program


So I try to make a program with JavaFX to upload pictures on ebay with the ebay trading api. I got the first request to get User consent working and I get a response code of 200, which is fine and all. But I have no clue on how to interact with the response. I got it to write to the console. But I need it to open a new window like a browser and after logging in and giving consent get the URL, so I can make a request for the access Token.

This is the code I use to make the request (I'm still very new to the whole api/ HTTP request stuff so, if there is an easier way I'm open for anything!)

import java.awt.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class Test {

    private final String USER_AGENT = "Mozilla/5.0";

    public static void main(String[] args) throws Exception {

        Desktop d = Desktop.getDesktop();
        d.browse(new URI("https://auth.sandbox.ebay.com/oauth2/authorize?client_id=ErikAlav-SandboxT-SBX-c1e8de676-5cd1c434&redirect_uri=Erik_Alaverdyan-ErikAlav-Sandbo-eohtlzvjq&response_type=code&state=HALLO_ES_HAT_GEKLAPPT&scope=https://api.ebay.com/oauth/api_scope"));

        Test http = new Test();

        System.out.println("Testing 1 - Send Http GET request");
        http.sendGet();


    }

    // HTTP GET request
    private void sendGet() throws Exception {

        String url = "https://auth.sandbox.ebay.com/oauth2/authorize?client_id=ErikAlav-SandboxT-SBX-c1e8de676-5cd1c434&redirect_uri=Erik_Alaverdyan-ErikAlav-Sandbo-eohtlzvjq&response_type=code&state=HALLO_ES_HAT_GEKLAPPT&scope=https://api.ebay.com/oauth/api_scope";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

    }
}

The result of this is just the html text of the site (If the exact code is needed I can supply that, but is for now I think way to much info and not important for now).

Thank you for your time!


Solution

  • EDIT: I just thought about it and saw that you can use a webviewer and display and at the same time get the URL. This video helped me the most: https://www.youtube.com/watch?v=P9z1dRPmeUQ

    (Hope you happy now ;D )