gpt4 book ai didi

java - com.google.api.client.googleapis.auth.oauth2.GoogleCredential 现已弃用

转载 作者:行者123 更新时间:2023-12-01 18:52:26 31 4
gpt4 key购买 nike

Google 的 sample code视觉 API 显示如下:

  public static Vision getVisionService() throws IOException, GeneralSecurityException {
GoogleCredential credential =
GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}

但是 com.google.api.client.googleapis.auth.oauth2.GoogleCredential 现已弃用。现在验证上述内容的正确方法是什么?

最佳答案

您可以通过几种方法来做到这一点。

选项 1:

  1. 按照此 quickstart page 上的步骤操作
  2. 设置客户端库默认使用的 GOOGLE_APPLICATION_CREDENTIALS

选项 2:

  1. 自行加载服务 key 文件。
// imports
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageAnnotatorSettings;

import java.io.FileInputStream;

// somewhere in your code, load the credentials
try {
Credentials credentials = GoogleCredentials.fromStream(
new FileInputStream("path_to_file.json"));

FixedCredentialsProvider fixedCredentialsProvider =
FixedCredentialsProvider.create(credentials);

ImageAnnotatorSettings settings =
ImageAnnotatorSettings.newBuilder()
.setCredentialsProvider(
fixedCredentialsProvider).build();

ImageAnnotatorClient client =
ImageAnnotatorClient.create(settings);
catch(IOException e) {
e.printStackTrace(e);
}

关于java - com.google.api.client.googleapis.auth.oauth2.GoogleCredential 现已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59710813/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com