问题
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