I am writing an application that uses the Spreadsheets API. Because I am using the write scope, which is sensitive, the OAuth2 redirect URI must use https instead of http (except when using localhost).
The credential object used in my API calls are fethed like this:
@Bean
public Credential credential(@Autowired LocalServerReceiver localServerReceiver) throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// Load client secrets from your credentials JSON file
InputStream credentialsStream = Talespire.class.getResourceAsStream(GOOGLE_SHEETS_CLIENT_SECRET_JSON);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(credentialsStream));
// Set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File("tokens")))
.setAccessType("offline")
.setApprovalPrompt("force")
.build();
return new AuthorizationCodeInstalledApp(flow, localServerReceiver).authorize("user");
}
The injected LocalServerReceiver is set up using the Builder it provides. In my dev environment, I set it up using localhost as host, which works fine since it does not require http. However, when moving to the production environment, there is no way to set the host (which is used to determine the callback URL) to use https-scheme. If it set host to https://app.comain.com, the resulting callback URL becomes http://https://app.domain.com/Callback which is obviously wrong. If I sneakily try to manually change the redirect-uri parameter in the authentication URL (to use https instead of http), I get an authorization-error (Error 400: bad_request) from Google when opening it in the browser. I am suspecting https require additional parameters or something, but I cannot find any documentation about it.
The LocalServerReveiver code (source https://github.com/googleapis/google-oauth-java-client/blob/main/google-oauth-client-jetty/src/main/java/com/google/api/client/extensions/jetty/auth/oauth2/LocalServerReceiver.java) is hardcoded to use the http-scheme (line 138), so I do not see a built-in method to set this up with the https-scheme, like specified.
Does anybody know how I can get around this?
Relevant libs/dependencies LocalServerReceiver: google-oauth-client-jetty-1.34.1.jar GoogleAuthorizatoinCodeFlow: google-api-client-1.35.2.jar AuthorizationCodeInstalledApp: google-oauth-client-java6-1.34.1.jar
edit: While reading through this post, it strikes me that perhaps the AuthorizationCodeInstalledApp is not the correct class to use to generate the Credential object here (based on its name and the fact that the dependency uses "java6" in its name). Could this be the case, and that I need to use a different class for this? And if so, which one should I use?