Google Cloud Vision API - 如何启用服务帐户
我想让我的脑袋围绕GoogleVision API Java库。Google Cloud Vision API - 如何启用服务帐户
我创建了一个服务帐号,下载了JSON和设置这个环境变量。使用
GOOGLE_APPLICATION_CREDENTIALS=C:\GoogleAPI\keys\translate-41428d4d1ec6.json
我已设置应用程序的默认凭据:
gcloud beta auth application-default login
我在这里下面的例子: https://cloud.google.com/vision/docs/reference/libraries
我现在假设提出将使用服务帐户作为所有呼叫如果通过魔术,因为我没有在代码中进行任何验证(按照示例)。
不过,我不能看到我的授权使用谷歌API的愿景,我想我要的服务帐户。所以,我也可以不使用服务帐户在所有...
当我进入IAM,并尝试指派“角色”服务帐户,还有什么相关的视觉API?有角色,如
- 我怎么能确定我使用的服务帐户拨打电话?
- 我需要做什么来明确启用服务帐户来访问一个特定的API,如GoogleVision当它在IAM心不是上市...或者可以与项目相关的访问API的所有服务帐户?
文档中的例子显示任何帮助如何
赞赏。
而且我很感兴趣,我将如何适应样品不使用应用程序的默认凭据,但实际上创建一个特定的凭证实例,并用它来调用谷歌愿景,因为这是不明确的。示例给出的是调用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,和无关的证书!
您无需授权服务帐户访问视觉API。在项目中启用API并使用与该项目关联的服务帐户就足够了。让我知道你是否仍然有问题。
谢谢,接下来我希望Vision在Google存储中处理图像,因此假设我需要使用服务帐户为存储桶分配ACL。向前 ... – smackenzie
您是否启用在https://console.cloud.google.com/apis/api/vision.googleapis.com/overview项目云愿景API?另请参阅与服务帐户相关的https://www.youtube.com/watch?v=tSnzoW4RlaQ。 – jarmod
我创建了一个服务帐号,启用了api。在任何时候,我都不得不专门为服务帐户提供视觉API?!服务帐户的创建是否足够?我能够使用没有角色的服务帐户...使用应用程序默认凭证来访问视觉API。我想我必须链接到服务帐户的API。 – smackenzie
据我所知,你不需要任何特定的角色来调用Vision API。我可以看到这可能是有意义的 - 您正在上载图像并要求对其进行分类,但您实际上并未使用任何需要权限的其他云资源。 – jarmod