Getting “The Application Default Credentials are not available” error in android studio even though env variable is set

时光总嘲笑我的痴心妄想 提交于 2020-06-29 04:22:56

问题


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]");

回答1:


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.




回答2:


How will the app find the resource file when inside a mobile phone? put it in the app/src/

put the file is in your classpath e.g. put the file in "src" folder, and make sure its moved in "bin" or "classes" folder after compilation:

URL fileURL = getClass().getClassLoader().getResource(fileName);
String fileName = fileURL.getFile();
String filePath = fileURL.getPath();

Also do note that doing this will work, but is extremely insecure and should not be done in production. Service account file is a critical file and should not be exposed. So i suggest you do whatever you need to do from your server side code instead of doing this




回答3:


You shouldn't use Google Could Vision API in an Android app. Instead, use ML Kit for Firebase.

to quote their docs:

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.

and see https://github.com/firebase/quickstart-android/tree/master/mlkit



来源:https://stackoverflow.com/questions/57407019/getting-the-application-default-credentials-are-not-available-error-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!