【问题标题】:User Consent Screen Shows Up After Access Token Expires访问令牌过期后显示用户同意屏幕
【发布时间】:2019-02-06 06:05:28
【问题描述】:

我正在尝试将 Google Calendar API 集成到我基于 Spring Boot 的 Google App Engine 项目之一中。使用基于 JAVA 的 google-client-api 库进行授权。我将在我的 App Engine 项目中创建的客户端 ID 和密码提供给 GoogleAuthorizationCodeFlow。在我第一次运行时,我得到了一个用户同意屏幕,接受它后,StoredCredential 文件在目标/令牌中创建。在此之后,我所有的 Google Calendar API 调用都可以正常工作。过了一段时间,我猜这与访问令牌到期有关,我再次获得用户同意屏幕。根据文档,如果访问类型设置为offline,授权流程将负责管理刷新令牌。 StoredCredential 文件仍然存在于目标/令牌路径中,但我仍然看到用户同意屏幕。粘贴授权码的sn-p。

  private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = EventController.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
        .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
        .setAccessType("offline")
        .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
  }

  public static Calendar get() throws GeneralSecurityException, IOException {
    // Build a new authorized API client com.huddle.processor.service.
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
        .setApplicationName(APPLICATION_NAME)
        .build();
  } 

更新: 在远程调试时,当 google-oauth-client 使用

从文件中获取存储的凭据时
StoredCredential stored = credentialDataStore.get(userId);

刷新令牌为空。想知道这是否是原因,但不确定为什么它没有设置。

如果有人可以让我知道我错过了什么,那就太好了。谢谢

【问题讨论】:

  • 删除用户的凭据并让他们再次登录。如果这不起作用,请转到用户的谷歌帐户并让他们删除您的应用程序的授权,然后再试一次。你应该得到一个访问令牌
  • 感谢@DaImTo,找到解决方案并在下方发布

标签: google-app-engine google-calendar-api google-api-java-client


【解决方案1】:

找到了解决方案。这与无法获取刷新令牌有关。需要传递approval_prompt=force 参数。所以更新后的代码 sn-p 是

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
        .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
        .setAccessType("offline")
        .setApprovalPrompt("force")
        .build();

对我有帮助的参考博客:https://googlecode.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 2018-06-05
    • 2021-08-25
    • 2013-12-25
    • 2021-12-19
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多