【问题标题】:com.google.api.client.googleapis.auth.oauth2.GoogleCredential is now deprecatedcom.google.api.client.googleapis.auth.oauth2.GoogleCredential 现在已弃用
【发布时间】:2020-01-13 05:04:19
【问题描述】:

Google 的视觉 API 的 sample code 显示了这一点:

  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 现在已弃用。现在验证上述内容的正确方法是什么?

【问题讨论】:

标签: java google-app-engine google-cloud-platform google-vision


【解决方案1】:

您可以通过多种方式做到这一点。

选项 1:

  1. 按照此quickstart page 上的步骤进行操作
  2. 设置GOOGLE_APPLICATION_CREDENTIALS,这是客户端库默认使用的东西

选项 2:

  1. 自己加载服务密钥文件。
// 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);
}

【讨论】:

    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2019-02-07
    • 2019-05-21
    • 2022-12-31
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多