This will require decision for scopes(service + scope , e.g- spreadsheet read only.) our app will be using.
Also, an end-point URL on our app, that will be used for redirection from Google auth server. This endpoint should support GET and POST methods and accept javax.servlet.http.HttpServletRequest along being ssl enabled.
1. Construct consent-request-URL and ask application user to hit same, so that user approves required access.
This URL consists of client-id, scope and redirection-URL(of our app). e.g- https://accounts.google.com/o/oauth2/v2/auth?scope=<SCOPE_TO_USE>&access_type=offline&include_granted_scopes=true&state=state_parameter_passthrough_value&redirect_uri=<REDIRECTION_URL>&response_type=code&client_id=<CLIENT_ID>
Google consent request will redirect user to redirection endpoint URL.
If User approves the consent requests, redirection will contain code else error.
2. Endpoint should should extract code from request object and POST another request for token generation to "https://www.googleapis.com/oauth2/v4/token".
This request will have client-id, client-secret, code, grant-type and same redirection URL.
This request returns json object as response body containing access_token.
3. Next we need to initialize desired (one sent in scope) Google API Service ( in our use case - com.google.api.services.sheets.v4.Sheets ) .
This uses com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(), com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance() and instance of com.google.api.client.googleapis.auth.oauth2.GoogleCredential with access token set.
later use service. In our application, obtain values from provided sheet id and cell ranges.
Sample project on github
search texts: read google spread sheet data from java, spring boot application.
Access google spreadsheet data from java, spring boot application.