【问题标题】:Migrate to GoogleCredentials from GoogleCredential从 GoogleCredential 迁移到 GoogleCredentials
【发布时间】:2022-11-08 22:55:00
【问题描述】:

目前,我正在使用以下实现,但代码显示 GoogleCredential 已弃用。

GoogleCredential credential = new GoogleCredential.Builder()
                    .setClientSecrets(clientId, clientSecret)
                    .setTransport(HTTP_TRANSPORT)
                    .setJsonFactory(JSON_FACTORY)
                    .build();
            credential.setRefreshToken(refreshToken);

我检查并应该使用 GoogleCredentials 或其他 google-auth-library 类。但是,它们似乎都需要服务帐户。 GoogleCredential 在没有服务帐户的情况下为我工作。只需要创建 oauth 凭据。我还生成了刷新令牌,但不确定如何将它们与新库一起使用。我应该在这里使用什么?目标是只允许单个用户(我们的后端代码)访问 google api。

我没有看到任何其他关于 java 的问题实际上得到了回答。

编辑-根据评论更新发布我的整个设置-

public Credentials getCredentials() throws GeneralSecurityException, IOException {

        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

        try(InputStream in = getCredentialsAsInputStream()) {

            if (in == null) {
                throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
            }
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

            String clientId = clientSecrets.getDetails().getClientId();
            String clientSecret = clientSecrets.getDetails().getClientSecret();

            Credentials credential = UserCredentials.newBuilder()
                    .setClientId(clientId)
                    .setClientSecret(clientSecret)
                    .setRefreshToken(refreshToken)
                    .build();

            return credential;

并用于设置驱动器

public Drive getDriveService() {
        try {
            Credentials credential = getCredentials();
            HttpRequestInitializer httpRequestInitializer = new HttpCredentialsAdapter(credential);
            final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
                    .setApplicationName(DRIVE_API_APPLICATION_NAME)
                    .setHttpRequestInitializer(httpRequest -> {

                        httpRequestInitializer.initialize(httpRequest);
                        httpRequest.setConnectTimeout(2 * 60000);  // 2 minutes connect timeout
                        httpRequest.setReadTimeout(2 * 60000);  // 2 minutes read timeout

                    })
                    .build();
        } catch (GeneralSecurityException | IOException e){
            log.error("Error creating drive service class : {}", e);
        }
        return null;
    }

【问题讨论】:

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


    【解决方案1】:

    尝试使用google-auth-library,因为它定义了类GoogleCredentials

    Google Credentials = new GoogleCredentials.Builder()
                .setClientSecrets(clientId, clientSecret)
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .build();
                credential.setRefreshToken(refreshToken);
    

    您也可以查看GoogleCredentials 的 Java 文档

    看来您正在尝试使用Drive API,因此您不妨检查此thread 关于连接到驱动器服务的信息。

    【讨论】:

    • 显然,构建器已保护访问,因此此方法不起作用。但是,我还注意到了一个 GoogleCredentials.newBuilder(),它没有附带“.setClientSecrets(clientId, clientSecret)”的选项。也欣赏另一个线程。我当然会尝试,但如前所述,我没有使用服务帐户。
    • 使用 UserCredentials 类有意义吗?我可以看到它有一个设置刷新令牌的选项
    猜你喜欢
    • 2021-08-02
    • 2018-07-02
    • 2020-12-01
    • 1970-01-01
    • 2012-12-04
    • 2011-04-26
    • 2015-07-23
    • 2020-05-31
    • 2010-10-26
    相关资源
    最近更新 更多