【问题标题】:Cancel google OAuth2 authorization取消谷歌 OAuth2 授权
【发布时间】:2021-06-29 15:24:21
【问题描述】:

我正在创建一个需要访问 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

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

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

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

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

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

【问题讨论】:

  • 过去的答案没有解决如何取消,但解释了它为什么会这样 - stackoverflow.com/a/38475819/775715
  • 我已经在异步调用authorize,但用户可能永远不会授权应用程序。
  • 也许升级? google-oauth-client-jetty(版本 1.31.5)的最新版本使用 Semaphore(而不是 Condition),现在专门在 LocalServerReceiver.stop() 上发布。
  • 我最初尝试使用它,但它导致了其他问题。但我会再试一次。谢谢。

标签: google-oauth jetty interrupt google-api-java-client


【解决方案1】:

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

这是一个错误,并在https://github.com/googleapis/google-oauth-java-client/issues/127作为问题打开

在 1.23.0 版中已解决

【讨论】:

    猜你喜欢
    • 2013-09-05
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多