获取“应用程序默认凭据不可用".即使设置了env变量,android studio中也会出现错误 [英] Getting "The Application Default Credentials are not available" error in android studio even though env variable is set

查看:62
本文介绍了获取“应用程序默认凭据不可用".即使设置了env变量,android studio中也会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在android studio中遇到问题.我已经设置了我的Google应用程序凭据环境变量,并一遍又一遍地重启了android studio,这给了我同样的错误.

我知道我为我的应用使用了正确的服务帐户 JSON 文件,并且知道设置了 GOOGLE_APPLICATION_CREDENTIALS 环境变量(该变量显示在我的android studio终端中和计算机的终端),我不知道为什么它不起作用.

即使设置了变量,当我执行"System.getenv(" GOOGLE_APPLICATON_CREDENTIALS)" 会返回 null .我也不知道为什么会这样.

以下是引发此错误的代码行:

GoogleCredential凭据= GoogleCredential.getApplicationDefault().fromStream(credentialsJSON).createScoped(VisionScopes.all());

我的 credentialsJSON 来自以下代码行:

  InputStream凭据JSON = LabelApp.class.getClassLoader().getResourceAsStream("C:\\ [服务授权路径-json]"); 

解决方案

您不能只在PC上设置 GOOGLE_APPLICATON_CREDENTIALS 并期望它们在您的智能手机上为Android ...(不等于Java),最好使用 Firebase ML工具包.

请参见 Firebase快速入门的机器学习套件 Android应用程序./p>

I'm currently having an issue in android studio. I have set my Google app credentials environment variable AND restarted android studio over and over and its giving me the same error.

I know I am using the correct service account JSON file for my app and I know the GOOGLE_APPLICATION_CREDENTIALS environment variable is set (The variable shows up in my android studio terminal AND my computer's terminal), I just have no clue why this isn't working.

Also even though the variable is set, when I do "System.getenv("GOOGLE_APPLICATON_CREDENTIALS")" it returns null. I don't knwo why this is either.

Here is the line of code throwing this error:

GoogleCredential credential = GoogleCredential.getApplicationDefault().fromStream(credentialsJSON).createScoped(VisionScopes.all());

My credentialsJSON comes from this line of code:

InputStream credentialsJSON = LabelApp.class.getClassLoader().getResourceAsStream("C:\\[path-to-service-acct-json]");

解决方案

You can't just set GOOGLE_APPLICATON_CREDENTIALS on your PC and expect them to be known on your smartphone... for Android (which does not equal Java), better use the ML Kit for Firebase.

See the ML Kit for Firebase Quickstart Android application.

这篇关于获取“应用程序默认凭据不可用".即使设置了env变量,android studio中也会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆