Java Google Drive API在服务器浏览器上显示同意屏幕

Java Google Drive API在服务器浏览器上显示同意屏幕,java,oauth-2.0,google-drive-api,google-oauth,google-api-java-client,Java,Oauth 2.0,Google Drive Api,Google Oauth,Google Api Java Client,这是我用来连接Google Drive API和验证用户身份的代码,我面临的问题是,每当我调用函数验证用户身份时,服务器(安装tomcat的地方,而不是用户使用的PC)上都会显示同意屏幕。我知道这是一个奇怪的问题,我在论坛上找不到这方面的任何东西 private Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException { final String CREDENTIALS_FI

这是我用来连接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");
}
private Credential getCredentials(最终NetHttpTransport HTTP_传输)引发IOException{
最终字符串凭据文件路径=“/CREDENTIALS.json”;
最终字符串标记\u目录\u路径=“/TOKENS”;
最终列表范围=Collections.singletonList(DriveScopes.DRIVE);
InputStream in=this.getClass().getResourceAsStream(凭证文件路径);
if(in==null){
抛出新的FileNotFoundException(“未找到资源:+凭证\文件\路径”);
}
GoogleClientSecrets clientSecrets=GoogleClientSecrets.load(JSON_工厂,新的InputStreamReader(in));
//生成流并触发用户授权请求。
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
HTTP_传输、JSON_工厂、客户端机密、作用域)
.setDataStoreFactory(新的FileDataStoreFactory(新的java.io.File(令牌\目录\路径)))
.setAccessType(“脱机”)
.build();
LocalServerReceiver=新的LocalServerReceiver.Builder().setPort(9999.build();
返回新的AuthorizationCodeInstalledApp(流程、接收方)。授权(“用户”);
}

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

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);
}