Upload videos to YouTube without entering a browser

I created a Java desktop application that allows you to share YouTube videos with a specific google account. I used the suggested code from the example to download the video: https://developers.google.com/youtube/v3/code_samples/java#upload_a_video

with user json token. But when I try to download it, the google web page opens and asks me to log in and approve the download of the video with my user.

Is there a way to skip this level and do it from java code? I do not want the user to log in to the google account on the webpage and use only the GUI created using java.

+4
source share
1 answer

You can copy class

AuthorizationCodeInstalledApp

and override the method

browse (String url)

You need to understand, go to the url programmatically, like this (I use htmlunit):

public void browse(String url) throws IOException { WebClient webClient = initWebClient(); HtmlPage htmlPage = webClient.getPage(url); //first you need login with your email and password final HtmlTextInput login = (HtmlTextInput) htmlPage.getByXPath("//input[@type='email']").get(0); final HtmlPasswordInput pass = (HtmlPasswordInput) htmlPage.getByXPath("//input[@type='password']").get(0); HtmlSubmitInput button = (HtmlSubmitInput) htmlPage.getByXPath("//input[@type='submit']").get(0); //set input login and passwd login.setText(this.login); pass.setText(this.passwd); //press submit button button.click(); //next need select account htmlPage = webClient.getPage(url); DomNodeList<HtmlElement> list = htmlPage.getElementById("account-list").getElementsByTagName("li"); String account = list.get(0).getElementsByTagName("a").get(0).getAttribute("href"); System.out.println(account); htmlPage = webClient.getPage(account); //and click submit button for approve System.out.println("Wait 10sec."); webClient.waitForBackgroundJavaScript(10000); HtmlButton submitInput = (HtmlButton) htmlPage.getElementById("submit_approve_access"); submitInput.click(); } 

This works great for me.

+3
source

Source: https://habr.com/ru/post/979540/


All Articles