Eug*_*nio 5 java google-cloud-platform google-vision
我假设我在写作之前已经用谷歌搜索并阅读了文档,我注意到这也是 StackOverflow 上的一个流行讨论,但已经给出的答案都没有帮助我。
我创建了一个 Google Cloud 帐户来使用 API:Google Vision。
为此,我按照创建项目、添加上述 API 并最终创建带有密钥的服务帐户的步骤进行操作。
我下载了密钥并将其放在PC上java项目的文件夹中。
然后,由于它是一个 Maven 项目,我按照教程中的描述将依赖项添加到了 pom 中。
此时,我插入了建议的代码段以开始使用 API。
一切似乎都很好,一切都被读取,各种库/接口都被导入。
但当我尝试运行该程序时,出现了错误:
应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,则它们可用。否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。
我必须承认我不知道“Google 计算引擎”是什么,但由于有替代方案并且我有一些凭据,所以我想尝试并遵循它。
所以我按照说明操作:
创建服务帐户后,您需要将服务帐户密钥下载到运行应用程序的计算机。您可以使用 GOOGLE_APPLICATION_CREDENTIALS 环境变量或编写代码将服务帐户密钥传递给客户端库。
好的,我尝试了第一种方法,通过环境变量传递凭据:
$env:GOOGLE_APPLICATION_CREDENTIALS="my key path"
Run Code Online (Sandbox Code Playgroud)
set GOOGLE_APPLICATION_CREDENTIALS=my key path
Run Code Online (Sandbox Code Playgroud)
所以我尝试了第二种方法,使用代码传递凭据,如果我在这里,一定是其他地方出了问题,事实上,用我所拥有的,只能导入 io.grpc.Context,而其他所有内容都会给出错误“无法解析符号..”。
import com.google.common.collect.Lists;
import io.grpc.Context;
import java.io.FileInputStream;
import java.io.IOException;
public class Main
{
static void authExplicit(String jsonPath)
{
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
.createScoped( Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Context.Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
}
public static void main(String[] args) throws IOException
{
OCR.detectText();
}
}
Run Code Online (Sandbox Code Playgroud)
(我不认为我可以上传代码屏幕来显示它在哪里给我带来错误,但就是这样)
PS:我也已经安装了Cloud SDK并重新启动了PC,因为有些评论说这样做可以修复问题。
解决方案
我不知道这一切是否成功,因为我在这个命令行中执行了一系列操作,无论如何,为了后代:
gcloud auth activate-service-account your_account_email_id --key-file=json_file_path
它还帮助我在之后重新启动电脑。
请考虑阅读类ImageAnnotationClient级别javadocs,它为您提供有关如何完成身份验证过程的正确指导。我修改了提供的代码,为您提供完整的示例:
// Please, set the appropriate path to your JSON credentials file here
String credentialsPath = "...";
// The credentials could be loaded as well as this.getClass().getResourceAsStream(), for example
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(credentialsPath))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
// Use that credentials to build your image annotation client
ImageAnnotatorSettings imageAnnotatorSettings =
ImageAnnotatorSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build()
;
ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings);
// Perform the stuff you need to...
Run Code Online (Sandbox Code Playgroud)
您需要在您的中提供的唯一依赖项pom.xml是:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>2.0.18</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
无论如何,请考虑参考pom.xmlCloud Vision 示例中提供的内容。
Cloud Vision 示例存储库在检测过程本身的Detect类中提供了一些有用的代码片段。
确保您用于连接到 Google Cloud 的服务帐号具有必要的权限。
尽管此解决方案可行,但它有一个缺点,即您可能需要将凭据文件存储在计算机中的某个位置,可能存储在源代码存储库中,等等,并且它可能存在安全风险。如果您从 Google Cloud 运行您的程序,始终建议您向您正在使用的虚拟机或服务授予必要的权限并使用默认的应用程序凭据。
使用 theGOOGLE_APPLICATION_CREDENTIALS也可能更好。如果您尝试使用此变量,首先尝试使用 IDE 提供的机制而不是 、 或 来配置它cmd,PS然后bash看看它是否有效。
对于使用最后提到的两个解决方案中的任何一个,您在构建您的ImageAnnotatorClient:
ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5819 次 |
| 最近记录: |