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

时间:2019-10-01 08:51:37

标签: 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_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)并显示同意屏幕。我不知道我在做什么错。感谢您的帮助。干杯!

1 个答案:

答案 0 :(得分:2)

if (mGoogleApiClient.hasConnectedApi(Auth.GOOGLE_SIGN_IN_API)) { mGoogleApiClient.clearDefaultAccountAndReconnect(); mGoogleApiClient.stopAutoManage(context); mGoogleApiClient.disconnect(); } 旨在供已安装的应用程序使用。

您应该使用的是GoogleAuthorizationCodeFlow.Builder

GoogleBrowserClientRequestUrl

#client-side_applications