即使设置了 env 变量,在 android studio 中出现 "The Application Default Credentials are not available" 错误
Getting "The Application Default Credentials are not available" error in android studio even though env variable is set
我目前在 android 工作室遇到问题。我已经设置了我的 Google 应用程序凭据环境变量,并一遍又一遍地重新启动 android 工作室,它给了我同样的错误。
我知道我正在为我的应用程序使用正确的服务帐户 JSON
文件并且我知道 GOOGLE_APPLICATION_CREDENTIALS
环境变量已设置(该变量显示在我的 android 工作室终端中和我电脑的终端),我只是不知道为什么这不起作用。
此外,即使设置了变量,当我这样做时
"System.getenv("GOOGLE_APPLICATON_CREDENTIALS")"
它 returns null。我也不知道这是为什么。
这是抛出此错误的代码行:
GoogleCredential credential = GoogleCredential.getApplicationDefault().fromStream(credentialsJSON).createScoped(VisionScopes.all());
我的credentialsJSON
来自这行代码:
InputStream credentialsJSON = LabelApp.class.getClassLoader().getResourceAsStream("C:\[path-to-service-acct-json]");
您不能只在您的 PC 上设置 GOOGLE_APPLICATON_CREDENTIALS
并期望它们在您的智能手机上被识别...对于 Android(不等于 Java),更好使用 ML Kit for Firebase.
查看 ML Kit for Firebase Quickstart Android 申请。
应用程序如何在移动设备中找到资源文件phone?把它放在 app/src/
将文件放在您的类路径中,例如将文件放在 "src" 文件夹中,并确保它在编译后移动到 "bin" 或 "classes" 文件夹中:
URL fileURL = getClass().getClassLoader().getResource(fileName);
String fileName = fileURL.getFile();
String filePath = fileURL.getPath();
另请注意,这样做会奏效,但极不安全,不应在生产环境中这样做。服务帐户文件是一个关键文件,不应公开。所以我建议你从服务器端代码做任何你需要做的事情,而不是这样做
您不应在 Android 应用中使用 Google Could Vision API。相反,请使用 ML Kit for Firebase。
引用他们的文档:
Mobile platform examples
These sample apps show how you can easily use the Cloud Vision label
detection, landmark detection, and text recognition APIs from your
mobile apps with ML Kit for Firebase. ML Kit also provides APIs to
perform face detection, barcode scanning, inference using custom ML
models, and more, all on the device, without requiring a network call.
并查看 https://github.com/firebase/quickstart-android/tree/master/mlkit
我目前在 android 工作室遇到问题。我已经设置了我的 Google 应用程序凭据环境变量,并一遍又一遍地重新启动 android 工作室,它给了我同样的错误。
我知道我正在为我的应用程序使用正确的服务帐户 JSON
文件并且我知道 GOOGLE_APPLICATION_CREDENTIALS
环境变量已设置(该变量显示在我的 android 工作室终端中和我电脑的终端),我只是不知道为什么这不起作用。
此外,即使设置了变量,当我这样做时
"System.getenv("GOOGLE_APPLICATON_CREDENTIALS")"
它 returns null。我也不知道这是为什么。
这是抛出此错误的代码行:
GoogleCredential credential = GoogleCredential.getApplicationDefault().fromStream(credentialsJSON).createScoped(VisionScopes.all());
我的credentialsJSON
来自这行代码:
InputStream credentialsJSON = LabelApp.class.getClassLoader().getResourceAsStream("C:\[path-to-service-acct-json]");
您不能只在您的 PC 上设置 GOOGLE_APPLICATON_CREDENTIALS
并期望它们在您的智能手机上被识别...对于 Android(不等于 Java),更好使用 ML Kit for Firebase.
查看 ML Kit for Firebase Quickstart Android 申请。
应用程序如何在移动设备中找到资源文件phone?把它放在 app/src/
将文件放在您的类路径中,例如将文件放在 "src" 文件夹中,并确保它在编译后移动到 "bin" 或 "classes" 文件夹中:
URL fileURL = getClass().getClassLoader().getResource(fileName);
String fileName = fileURL.getFile();
String filePath = fileURL.getPath();
另请注意,这样做会奏效,但极不安全,不应在生产环境中这样做。服务帐户文件是一个关键文件,不应公开。所以我建议你从服务器端代码做任何你需要做的事情,而不是这样做
您不应在 Android 应用中使用 Google Could Vision API。相反,请使用 ML Kit for Firebase。
引用他们的文档:
Mobile platform examples
These sample apps show how you can easily use the Cloud Vision label detection, landmark detection, and text recognition APIs from your mobile apps with ML Kit for Firebase. ML Kit also provides APIs to perform face detection, barcode scanning, inference using custom ML models, and more, all on the device, without requiring a network call.
并查看 https://github.com/firebase/quickstart-android/tree/master/mlkit