【问题标题】:Google Drive API shows consent screen on server browserGoogle Drive API 在服务器浏览器上显示同意屏幕
【发布时间】:2020-01-30 12:51:16
【问题描述】:

所以这是我用于连接到 Google Drive API 并验证我的用户的代码,我面临的问题是,每当我调用该函数来验证用户身份时,同意屏幕就会显示在服务器上(其中安装的是tomcat,而不是用户正在使用的PC)。我知道这是一个奇怪的问题,我在论坛中找不到任何关于此的内容。

private Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {

    final String CREDENTIALS_FILE_PATH = "/credentials.json";
    final String TOKENS_DIRECTORY_PATH = "/tokens";
    final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);

    InputStream in = this.getClass().getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: " + 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(9999).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

调用此方法后,应用程序正在打开服务器的默认浏览器(Chrome)并显示同意屏幕。我不知道我在这里做错了什么。感谢您的帮助。干杯!

【问题讨论】:

    标签: java oauth-2.0 google-drive-api google-oauth google-api-java-client


    【解决方案1】:

    GoogleAuthorizationCodeFlow.Builder 旨在供已安装的应用程序使用。

    你应该使用的是GoogleBrowserClientRequestUrl

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
      String url = new GoogleBrowserClientRequestUrl("812741506391.apps.googleusercontent.com",
          "https://oauth2.example.com/oauthcallback", Arrays.asList(
              "https://www.googleapis.com/auth/userinfo.email",
              "https://www.googleapis.com/auth/userinfo.profile")).setState("/profile").build();
      response.sendRedirect(url);
    }
    

    #client-side_applications

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2017-05-06
      • 2019-08-08
      • 2020-10-11
      • 1970-01-01
      相关资源
      最近更新 更多