Google Cloud Vision API - 如何启用服务帐户

Google Cloud Vision API - How to enable a service account

我正在努力让我的头圆满 Google愿景 API Java 图书馆。

我已经创建了一个服务帐户,下载了 json 并设置了这个环境变量。

GOOGLE_APPLICATION_CREDENTIALS=C:\GoogleAPI\keys\translate-41428d4d1ec6.json

我已使用以下方法设置应用程序默认凭据:

gcloud beta auth application-default login

我正在按照此处的示例进行操作: https://cloud.google.com/vision/docs/reference/libraries

我现在假设所有调用都将使用服务帐户,就像变魔术一样,因为我没有在代码中进行任何身份验证(根据示例)。

但是,我看不到我在哪里授权服务帐户使用 Google Vision API,我想我必须这样做。所以,我可能根本不使用服务帐户...

当我进入 IAM 并尝试将 "role" 分配给服务帐户时,没有任何与愿景相关的内容 API?有

等角色

文档中的示例说明了如何

感谢任何帮助。

此外,我对如何调整示例以不使用应用程序默认凭据,而是实际创建一个特定的凭据实例并使用它来调用 Google Vision 感兴趣,因为这还不清楚。给出的示例用于调用 GoogleStorage,但我无法将其转换为 GoogleVision。

public class StorageFactory {
  private static Storage instance = null;

  public static synchronized Storage getService() throws IOException, GeneralSecurityException {
    if (instance == null) {
      instance = buildService();
    }
    return instance;
  }

  private static Storage buildService() throws IOException, GeneralSecurityException {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    // Depending on the environment that provides the default credentials (for
    // example: Compute Engine, App Engine), the credentials may require us to
    // specify the scopes we need explicitly.  Check for this case, and inject
    // the Cloud Storage scope if required.
    if (credential.createScopedRequired()) {
      Collection<String> scopes = StorageScopes.all();
      credential = credential.createScoped(scopes);
    }

    return new Storage.Builder(transport, jsonFactory, credential)
        .setApplicationName("GCS Samples")
        .build();
  }
}

不要让我开始浪费我刚刚浪费的 2 个小时 "Access Denied"...这显然是由于图像大小超过 4mb,与凭据无关!

您无需授权服务​​账号访问视觉API。在您的项目中启用 API 并使用与该项目关联的服务帐户就足够了。如果您仍有问题,请告诉我。