【发布时间】:2017-06-29 18:58:27
【问题描述】:
我正在尝试了解 GoogleVision 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 并尝试为服务帐户分配“角色”时,与视觉 API 没有任何关系吗?有
等角色- 如何确定我使用的是服务帐号拨打电话?
- 当服务帐户未在 IAM 中列出时,我需要明确哪些内容才能访问特定 API(例如 GoogleVision)......或者与项目相关的所有服务帐户都可以访问 API?
文档中的示例显示了如何
任何帮助表示赞赏。
我还对如何调整示例以不使用应用程序默认凭据感兴趣,但实际上创建了一个特定的凭据实例并使用它来调用 Google Vision,因为这还不清楚。给出的示例是调用 GoogleStorage,但我无法将其转换为 Google Vision。
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 个小时内开始“拒绝访问”...这显然是由于图像大小超过 4mb,与凭据无关!
【问题讨论】:
-
您是否在console.cloud.google.com/apis/api/vision.googleapis.com/… 为您的项目启用了 Cloud Vision API?另请参阅与服务帐户相关的 youtube.com/watch?v=tSnzoW4RlaQ。
-
我创建了一个服务帐户,启用了 api。在任何时候我都不必专门向服务帐户授予对视觉 API 的访问权限?!创建服务帐户是否足够?我可以使用没有角色的服务帐户...访问视觉 API...使用应用程序默认凭据。我想我必须将服务帐户链接到 API。
-
据我所知,调用 Vision API 不需要任何特定角色。我可以看到这可能有什么意义 - 您正在上传图像并要求对其进行分类,但您实际上并没有使用任何其他需要权限的云资源。
-
感谢您的帮助
标签: java google-cloud-platform google-cloud-vision