I am creating a java desktop app that needs access to Google APIs.
I authorize the application like this:
Details credentialDetails=...;
List<String> scopes=...;
GoogleClientSecrets clientSecrets=new GoogleClientSecrets();
clientSecrets.setInstalled(credentialDetails);
LocalServerReceiver receiver=new LocalServerReceiver.Builder().setPort(8888).build();
NetHttpTransport httpTransport=GoogleNetHttpTransport.newTrustedTransport();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JacksonFactory.getDefaultInstance(), clientSecrets, scopes)
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver)
.authorize("user");
authorize()
then waits until the authorization finishes using LocalServerReceiver#waitForCode
that uses Condition#awaitUninterruptibly
.
The problem is that the user could e.g. close the browser window and ignore the authorization and I need to cancel it (from another thread).
I tried using receiver.stop();
but while stop()
is executed, the Condition
is not signal()
ed and the thread doing the authorization stays stuck in Condition#awaitUninterruptibly
.
Normal interruption also doesn't work as the LocalServerReceiver#waitForCode
uses Condition#awaitUninterruptibly
.
How can I cancel the OAuth2 authorization from another thread?
I am using com.google.oauth-client:google-oauth-client-jetty:1.22.0
.
With version 1.22.0 you cannot use LocalServerReceiver#stop()
to cancel the pending authorize()
.
This is a bug, and was opened as an issue at https://github.com/googleapis/google-oauth-java-client/issues/127
Which was addressed in version 1.23.0