取消google OAuth2授权

Cancel google OAuth2 authorization

我正在创建一个需要访问 Google API 的 java 桌面应用程序。

我这样授权应用:

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() 然后等待使用 LocalServerReceiver#waitForCode 完成授权,使用 Condition#awaitUninterruptibly.

问题是用户可以,例如关闭浏览器 window 并忽略授权,我需要取消它(从另一个线程)。

我尝试使用 receiver.stop();,但是当 stop() 被执行时,Condition 没有被 signal()ed 并且执行授权的线程停留在 Condition#awaitUninterruptibly.

正常中断也不起作用,因为 LocalServerReceiver#waitForCode 使用 Condition#awaitUninterruptibly

如何取消另一个线程的OAuth2授权?

我正在使用 com.google.oauth-client:google-oauth-client-jetty:1.22.0

在版本 1.22.0 中,您不能使用 LocalServerReceiver#stop() 取消待处理的 authorize()

这是一个错误,已在 https://github.com/googleapis/google-oauth-java-client/issues/127

作为问题打开

版本 1.23.0 中解决了这个问题