i am using java library client for web application authentication, i produce authorization url using client secret and client id,also i provided a redirect url within google api console,but i don't know if it is necessary for me to create this server to receive refresh token? i mean in production i should provide a separate server to receive the refresh token?(redirect url comes to this server) the main problem is user should paste the produced url on browser by himself but i want to open browser authmaticly , the second one is about reciving the refresh token i am not sure about creating another server to recieve refreshcode and i can't use service accounts i am going with web flow authentication.

 UserAuthorizer userAuthorizer =                 UserAuthorizer.newBuilder()                         .setClientId(ClientId.of(clientId, clientSecret))                         .setScopes(SCOPES)                         .setCallbackUri(URI.create(OAUTH2_CALLBACK_URL_CONFIGURED_AT_GOOGLE_CONSOLE))                         .build();         baseUri = URI.create("http://localhost:" + simpleCallbackServer.getLocalPort());         System.out.printf(                 "Paste this url in your browser:%n%s%n",                 userAuthorizer.getAuthorizationUrl(loginEmailAddressHint, state, baseUri)); 

and this is local server to receive refresh token:

private static class SimpleCallbackServer extends ServerSocket {         private AuthorizationResponse authorizationResponse;         SimpleCallbackServer() throws IOException {             // Passes a port # of zero so that a port will be automatically allocated.             super(0);         }         /**          * Blocks until a connection is made to this server. After this method completes, the          * authorizationResponse of this server will be set, provided the request line is in the          * expected format.          */         @Override         public Socket accept() throws IOException {             Socket socket = super.accept();         } } 

Tag:google-ads-api, spring-boot, oauth-2.0

Only one comment.

  1. saba

    for those who struggling to get authorized using google oauth2.0 with spring boot you cant redirect user to authorization url(which google authorization server gives to using your client id and client secret) use a controller to redirect user:

    @GetMapping(value = "/redirect-user") public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException { String url=gs.createUserAuthorizationUrl(); URI authorizationUrl = new URI(url); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(authorizationUrl); return new ResponseEntity<>(httpHeaders, HttpStatus.FOUND); }

    at service layer createUserAuthorizationUrl() method is like below:

    public String createUserAuthorizationUrl() { clientId = "client-id"; clientSecret = "client-secret-code"; userAuthorizer = UserAuthorizer.newBuilder() .setClientId(ClientId.of(clientId, clientSecret)) .setScopes(SCOPES) .setCallbackUri(URI.create("/oauth2callback")) .build(); baseUri = URI.create("your-app-redirect-url-configured-at-google-console" + "your-spring-boot-server-port"); //giving redirect url String redirectURL = userAuthorizer.getAuthorizationUrl(loginEmailAddressHint, state, baseUri).toString(); return redirectURL; }

    and let's create a controller to support the get request comming from google authorization server with an code. we are going to use that code to get access token from google.i get state and code by @RequestParam and i also want to redirect user to my application.

    @GetMapping(value = "/oauth2callback") public ResponseEntity<Object> proceedeTOServer(@RequestParam String state, @RequestParam String code) throws URISyntaxException { String url="my-application-url-to-redirect-user"; URI dashboardURL = new URI(url); HttpHeaders httpHeaders=new HttpHeaders(); httpHeaders.setLocation(dashboardURL); gs.getCode(state,code); return new ResponseEntity<>(httpHeaders,HttpStatus.FOUND); }

    and in getCode(code) in service layer i am going to send to code and receive the refresh token or access token:

    UserCredentials userCredentials =userAuthorizer.getCredentialsFromCode(code, "your-app-redirect-url-configured-at-google-console" + "your-spring-boot-server-port");

Add a new comment.