How to automate the extraction of OAuth code from a user in a standalone program

Can someone help us run the URL through java code:

We are trying to upload a file from our local drive to Gmail Drive.

Next steps

  • Generated URL using Google Developer (API)

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE)) .setAccessType("online") .setApprovalPrompt("auto").build(); String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build(); 
  • URL below

    https://accounts.google.com/o/oauth2/auth?access_type=online&approval_prompt=auto&client_id=1066028402320.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/drive

  • Run the url in an internet browser

  • The user password and password are set as an input in the Internet browser to receive a unique response token

Now, as part of our development, we have finished up to step 2 and want to automate steps 3 and 4 using java code. (After generating the URL provided by our UserdId and password, we should receive the response as a unique token.)

Waiting for your help on this

+4
source share
1 answer

I would use the following script

  • Configure a local web server to retrieve code from oauth redirect user
  • Set the redirect_uri stream for the local web server and get the auth url
  • Open auth url browser for user
  • Get the code from the local web server and exchange the oauth code

Here are some details with the code.

Configure a local web server to retrieve an HTTP request

Below is an example of setting up a local web server with NanoHttpd

 public class OAuthServer extends NanoHTTPD { /** * Constructs an HTTP server on given port. */ public DebugServer() { super(8080); } @Override public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) { bool error = false string code = null // User rejected approval if (parm.containsKey("error")) { error = true } // Here we get the code! if (parm.containsKey("code")) { code = parm.get("code") } StringBuilder sb = new StringBuilder(); sb.append("<html>"); sb.append("<head><title>Authorization</title></head>"); sb.append("<body>"); if (error) { sb.append("<h1>User rejected</h1>"); } if (code==null) { sb.append("<h1>Unknown Error</h1>"); } else { sb.append("<h1>Success</h1>"); } sb.append("</body>"); sb.append("</html>"); return new Response(sb.toString()); } public static void main(String[] args) { ServerRunner.run(OAuthServer.class); } } 

Set the redirect_uri stream for the local web server and get the auth url

 String url = flow.newAuthorizationUrl().setRedirectUri("http://localhost:8080").build(); 

Open auth url browser for user

 // open the default web browser for the HTML page Desktop.getDesktop().browse(url); 

Get the code from the local web server and exchange the oauth code

Now the user will approve OAuth from the web browser and send the code to the local web server that we just started. Now that we have the code extracted from the local web server, we can analyze it in int and authenticate and authorize with it!

Hope this helps

+2
source

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


All Articles